4. File and directory operations for CAE
Every CAE solver run is surrounded by file movement. A typical explicit dynamics job needs a mesh file, a material database, a load curve table, and a solver control deck in one working directory before the solver starts; when it finishes, the .odb, .dat, .sta, and restart files must be moved somewhere durable before the next run overwrites them. Windows batch scripting handles this with five core commands: COPY, MOVE, DEL, MKDIR, and REN. Used carelessly, they silently destroy data; used deliberately, they turn a fragile manual workflow into a repeatable one.
The five commands and what they actually do
These commands are internal cmd.exe built-ins, so they are always available and need no external executable. Their semantics differ in ways that matter for solver automation:
| Command | Primary effect | Key switches for CAE use |
|---|---|---|
COPY | Duplicates a file; source remains in place | /Y overwrite without prompt, /B binary mode, /V verify |
MOVE | Relocates a file or directory; source is removed | /Y overwrite without prompt |
DEL | Deletes files (not directories) | /Q quiet, /F force read-only, /S recurse |
MKDIR | Creates a directory, including intermediate levels | none needed; MD is identical |
REN | Renames within the same directory | none; cannot move across directories |
Two distinctions cause most beginner errors. First, COPY preserves the original, so a staging step that uses COPY leaves the master input untouched — usually what you want. MOVE removes the source, so it is the right tool for archiving but the wrong tool for staging from a shared library. Second, REN only changes the name; it cannot relocate a file to another folder. If you need both a new name and a new location, use MOVE with a full destination path.
Staging inputs: build the run directory first
A solver will not create its own working directory. The staging block therefore begins with MKDIR, which creates intermediate levels in one call. The following example stages a crash analysis job named JOB_1042 from a shared input library into a per-run folder:
@echo off
setlocal
set JOB=JOB_1042
set LIB=\\cae-share\library\crash
set RUN=D:\runs\%JOB%
mkdir "%RUN%" 2>nul
mkdir "%RUN%\logs" 2>nul
copy /Y /B "%LIB%\mesh_%JOB%.inp" "%RUN%\mesh.inp"
copy /Y /B "%LIB%\materials.mat" "%RUN%\materials.mat"
copy /Y /B "%LIB%\curves\load.csv" "%RUN%\load.csv"
copy /Y /B "%LIB%\solver.ctrl" "%RUN%\solver.ctrl"
if errorlevel 1 (
echo STAGING FAILED for %JOB% >> "%RUN%\logs\stage.log"
exit /b 1
)
echo Staged %JOB% at %DATE% %TIME% >> "%RUN%\logs\stage.log"
endlocal
Three details deserve attention. The 2>nul after each mkdir suppresses the "subdirectory already exists" message, which is harmless but noisy in a log. The /B switch forces binary copy, which matters for .odb, .sim, and any compressed input — text-mode copy can corrupt byte sequences. Finally, errorlevel is checked after the copy block; COPY sets it to 1 when a source file is missing, so a single check catches any absent input. Note that if errorlevel 1 means "greater than or equal to 1", not "equal to 1".
\\cae-share\library, run the script under an account that has read access. A failed COPY from an unreachable share returns errorlevel 1 with the message "The network path was not found" — the same code as a missing file, so log the raw output if you need to distinguish them.
Archiving results: move, do not copy
After the solver exits, results should be relocated to an archive tree keyed by job and timestamp. MOVE is correct here because it removes the working copy and prevents the next run from reading stale output. A common pattern is to archive into a dated folder:
set ARCH=D:\archive\%JOB%\%DATE:~-4%%DATE:~-10,2%%DATE:~-7,2%
mkdir "%ARCH%" 2>nul
move /Y "%RUN%\*.odb" "%ARCH%\" >nul
move /Y "%RUN%\*.sta" "%ARCH%\" >nul
move /Y "%RUN%\*.dat" "%ARCH%\" >nul
move /Y "%RUN%\restart_*" "%ARCH%\" >nul
The %DATE:~-4%%DATE:~-10,2%%DATE:~-7,2% substring extracts a YYYYMMDD stamp from the locale-dependent %DATE% variable. This is fragile across regional settings; a more robust approach uses wmic os get localdatetime or a fixed counter. Whichever you choose, verify the resulting folder name once before trusting it in production.
Wildcards in MOVE and COPY expand against the source directory. If no file matches, MOVE reports "The system cannot find the file specified" and sets errorlevel 1 — useful for detecting a solver that produced no output, but also a source of false alarms when a job legitimately has no restart files. Guard optional patterns with a conditional or accept the errorlevel and continue.
Cleanup and renaming
DEL removes files but never directories; use RMDIR /S /Q for folders. Before deleting, confirm the target is what you think it is. A safe cleanup block deletes only known temporary extensions inside the run directory:
del /Q "%RUN%\*.tmp" 2>nul
del /Q "%RUN%\*.lck" 2>nul
del /Q "%RUN%\solver_scratch\*.*" 2>nul
REN is useful when a solver writes a fixed output name that must be preserved per run. For example, if the solver always emits results.odb, rename it immediately after the run so the next job cannot overwrite it:
ren "%RUN%\results.odb" "results_%JOB%.odb"
Because REN cannot cross directories, the file stays in the run folder until the archive MOVE relocates it. This two-step pattern — rename in place, then move — keeps the archive naming consistent even when the solver's output name is fixed.
DEL /S and RMDIR /S recurse into subdirectories. A mistyped variable such as del /S /Q "%RUN%\*.*" where %RUN% is empty expands to del /S /Q "\*.*" and can delete from the current drive root. Always quote paths and validate that %RUN% is non-empty before any recursive delete.
How the operations fit together
The diagram below shows the lifecycle of a single job: inputs are copied from the shared library into a fresh run directory, the solver consumes them, outputs are renamed in place, and results are moved to a dated archive while scratch files are deleted.
Verification and failure symptoms
After staging, confirm the run directory contains exactly the expected files before launching the solver. A quick check uses dir /B and compares the count:
for /f %%C in ('dir /B "%RUN%\*.inp" ^| find /c /v ""') do set N=%%C
if not "%N%"=="1" (
echo Expected 1 .inp file, found %N% >> "%RUN%\logs\stage.log"
exit /b 2
)
Typical failure symptoms and their causes: a solver that starts but reports "input file not found" usually means COPY failed silently because the source path was wrong — check errorlevel. An archive folder that is empty after a successful run usually means the MOVE wildcard matched nothing because the solver wrote a different extension than expected. A run that produces results but overwrites the previous job's output indicates the REN step was skipped or the archive MOVE targeted the wrong directory. Each of these is diagnosable from the staging and archive log lines, which is why every block above appends a timestamped entry.
The discipline that makes these five commands safe is simple: create directories before writing into them, copy when the source must survive, move when it must not, delete only after archiving, and rename before the next run can collide. Applied consistently, they reduce a solver workflow from a sequence of manual drag-and-drop operations to a script that either completes cleanly or fails with a log entry pointing at the exact missing file.