Use this when writing or fixing .bat/.cmd scripts.
Batch Files
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.
1. Run it in a console to see errors.
2. @echo on temporarily to trace.
3. Use %~dp0 for the script's own folder.
4. Check %ERRORLEVEL% after each critical step (if errorlevel 1 exit /b 1).
5. Scheduled task → full paths and the right account.
batch file read line by line → variables expanded → commands run → exit /b code
Context & applicabilityCommon mistakes · platform/version boundaries
- Variables inside
( )blocks expanded at parse time — usesetlocal EnableDelayedExpansionand!var!. - Spaces around
=inset x = 1(creates a variable namedxwith value1). - Not checking
%ERRORLEVEL%after critical commands. - Using batch for complex logic — move to PowerShell.
Operational reference
Fast context before you go deeper.
Evidence · 1 on this Topic
Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.
- Use setlocal EnableDelayedExpansion and !var! inside loops.
- Check %ERRORLEVEL% after each important command.
- Quote every path that may contain spaces.
echo %PATH%— PATH in CMDset— environment variableswhere <name>— command resolutionecho %ERRORLEVEL%— exit codecmd /c script.bat— run a batch file and return
ReferenceWhy it matters1 section · depth on demand
Batch basics
@echo off
setlocal enabledelayedexpansion
set LOG=C:\logs\job.log
for %%f in (C:\in\*.csv) do (
copy "%%f" D:\archive\ >> "%LOG%"
if errorlevel 1 echo Failed: %%f >> "%LOG%"
)
exit /b 0
Documented example, not captured output. Use %% for loop variables in files (% on the command line), quote paths with spaces, and return exit codes with exit /b. New automation is better written in PowerShell.
ReferenceImportant terms & boundaries2 sections · depth on demand
Error handling pattern
@echo off
setlocal
robocopy C:\src D:\dst /MIR /R:2 /W:5 /LOG:C:\logs\copy.log
if %ERRORLEVEL% GEQ 8 (
echo Copy failed with %ERRORLEVEL%
exit /b 1
)
exit /b 0
Documented example, not captured output. Robocopy exit codes below 8 mean success (with or without copied files).
In depth
Batch file essentials:
@echo off
setlocal EnableExtensions EnableDelayedExpansion
set LOG=%~dp0logs\run_%DATE:~-4%%DATE:~3,2%%DATE:~0,2%.log
if not exist "%~dp0logs" mkdir "%~dp0logs"
for /f "usebackq tokens=*" %%S in ("%~dp0servers.txt") do (
ping -n 1 %%S > NUL
if !ERRORLEVEL! equ 0 (echo %%S up>>"%LOG%") else (echo %%S DOWN>>"%LOG%")
)
exit /b 0
%~dp0 is the script's folder; %% doubles loop variables in files; %DATE% formats depend on locale (use PowerShell Get-Date -f for reliable timestamps). New automation belongs in PowerShell.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Verification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
Authoritative/upstream documentation reviewed for bounded claims in Command Prompt → Batch Files. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/windows-commands · confidence 94%