Pcornel
Browse topics
Options
Recent
Favorites
Windows · Technical reference

Batch Files

Direct 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.

When to use this

Use this when writing or fixing .bat/.cmd scripts.

First 30 seconds

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.

Operational verification

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

Context & applicabilityCommon mistakes · platform/version boundaries
Common mistakes
Common mistakes
  • Variables inside ( ) blocks expanded at parse time — use setlocal EnableDelayedExpansion and !var!.
  • Spaces around = in set x = 1 (creates a variable named x with value 1).
  • Not checking %ERRORLEVEL% after critical commands.
  • Using batch for complex logic — move to PowerShell.
Platform / version applicability
Windows execution, automation and observability
applies
Structured applicability is explicit Topic metadata; it does not infer compatibility beyond the stored rows.
At a glance

Operational reference

Fast context before you go deeper.

PlatformWindows execution, automation and observability
Scope / prerequisiteUse this when writing or fixing .bat/.cmd scripts.
What next?2 suggested next steps
Evidence1
On this pageJump to a specific section
Evidence references

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.

observation · 1
Key points
  • Use setlocal EnableDelayedExpansion and !var! inside loops.
  • Check %ERRORLEVEL% after each important command.
  • Quote every path that may contain spaces.
Key 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
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.

Source basisSource attribution states what this section is based on; it is not independent proof that the current environment matches it.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Technical verification history

Verification history & basis

When and against what this Topic was checked. Later text edits do not count as a new check.

2026-09-14 · vendor docWindows execution, automation and observability current

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%
Confirmation

Confirm action

Continue?