Pcornel
Windows · Command Prompt

Batch Files

Cheat sheet · https://pcornel.dk/topic.php?topic=3058

Back to the Topic

Answer

Batch files (.bat or .cmd) are scripts of cmd commands with variables, IF, FOR loops, GOTO and CALL. They suit simple automation; use PowerShell for anything more complex.

Remember

  • Use setlocal EnableDelayedExpansion and !var! inside loops.
  • Check %ERRORLEVEL% after each important command.
  • Quote every path that may contain spaces.

First checks

  • echo %PATH% — PATH in CMD
  • set — environment variables
  • where <name> — command resolution
  • echo %ERRORLEVEL% — exit code
  • cmd /c script.bat — run a batch file and return

Verify in this order

batch file read line by line → variables expanded → commands run → exit /b code

Where it usually breaks

First mismatch in definition/configuration → security context → service/task/process creation → runtime state → output/side effect → event/performance evidence; preserve identity/native error/pre-state before mutation.

Read-only verification commands unless stated otherwise. Confirm syntax for your platform and release. Full reference: https://pcornel.dk/topic.php?topic=3058

Confirmation

Confirm action

Continue?