Batch Script Quick Reference
This reference collects the syntax that comes up in nearly every batch script: declaring variables, branching with IF, looping with FOR, and reading command-line arguments. It's meant to be scanned rather than read top to bottom — jump to the section covering the construct you need, copy the pattern, and adapt the variable names, the same way you'd flip to a specific page in a field manual rather than reading it cover to cover.
Cricket analogy: A fielding coach flipping straight to the slip-catching drill page in a coaching manual rather than reading the whole book mirrors jumping directly to the FOR loop section of this reference.
Variables, Strings, and Arithmetic
Set a variable with 'set "NAME=value"'; read it with %NAME% for normal expansion or !NAME! inside a delayed-expansion block. Do arithmetic with 'set /a RESULT=5+3' — note there are no spaces required around operators but the variable is always treated as an integer. String substitution uses '%VAR:old=new%' to replace text, and substring extraction uses '%VAR:~start,length%' — for example '%VAR:~0,3%' takes the first three characters, while '%VAR:~-4%' takes the last four.
Cricket analogy: Substituting a specific fielder's position mid-over ('%VAR:old=new%' style) the way a captain swaps deep midwicket for a short leg for one delivery mirrors targeted string substitution in a variable.
@echo off
set "name=World"
set /a x=5+3
echo Hello, %name%! x=%x%
set "path=C:\Users\Public\Documents"
echo First 2 chars: %path:~0,2%
echo Last 9 chars: %path:~-9%
echo Replace: %path:Public=Shared%Control Flow and Exit Codes at a Glance
Comparisons use IF with /I for case-insensitive string matches, or EQU/NEQ/GEQ/LEQ/GTR/LSS for numeric comparisons — 'if /i "%mode%"=="prod"' versus 'if %count% GTR 10'. Loop constructs are FOR %%x in (1 2 3) for a fixed list, FOR /L %%i in (1,1,10) for a numeric range with start,step,end, and FOR /D for directories. Exit codes follow the convention that 0 means success and any nonzero value signals an error to the caller; use 'exit /b N' to set that code when leaving a script or subroutine.
Cricket analogy: A DRS system distinguishing between an 'umpire's call' and a clear overturn uses different thresholds the way IF uses EQU versus GEQ for different numeric comparison intents.
Quick lookup — numeric comparison operators for IF: EQU (equal), NEQ (not equal), LSS (less than), LEQ (less or equal), GTR (greater than), GEQ (greater or equal). These exist because == in IF performs a string comparison, and '9' would sort before '10' as strings without them.
FOR /L %%i in (1,1,10) counts inclusively from 1 to 10 — ten iterations, not nine. A common off-by-one mistake is assuming the end value is exclusive, as it would be in languages like Python's range().
- set "NAME=value" declares a variable; %NAME% expands normally, !NAME! expands with delayed expansion enabled.
- set /a performs integer arithmetic; %VAR:~start,length% and %VAR:old=new% handle substrings and replacement.
- Use EQU/NEQ/GTR/LSS/GEQ/LEQ for numeric IF comparisons since == compares strings.
- FOR %%x in (list) iterates fixed values; FOR /L %%i in (start,step,end) iterates a numeric range inclusively.
- if /i performs case-insensitive string comparisons.
- exit /b N sets the script or subroutine's exit code; 0 conventionally means success.
- FOR /L's end value is inclusive, unlike range-style iteration in many other languages.
Practice what you learned
1. What does %PATH:~0,2% extract from a variable named PATH?
2. Why does 'if %count%==10' behave unexpectedly compared to 'if %count% EQU 10' when count could be '9' or '10'?
3. How many times does 'for /L %%i in (1,1,10) do echo %%i' execute?
4. What does 'exit /b 1' communicate to a script or process that called this batch file?
5. What is the effect of 'if /i "%mode%"=="PROD"' when %mode% holds the value 'prod'?
Was this page helpful?
You May Also Like
Common Batch Script Patterns
A catalog of reusable Windows batch scripting idioms — looping over files, parsing command output, handling arguments, and structuring error paths.
Batch Script Best Practices
Conventions that keep Windows batch files readable, safe, and maintainable — from quoting discipline to structured error handling.
Batch Script Interview Questions
A study guide covering the Windows batch scripting concepts most commonly probed in technical interviews, from variable scoping to error codes.
Related Reading
Related Study Notes in Microsoft Technologies
Browse all study notesWindows 10 / UWP Development Study Notes
.NET · 30 topics
Microsoft TechnologiesMFC (Microsoft Foundation Classes) Study Notes
C++ · 30 topics
Microsoft TechnologiesSilverlight Study Notes
.NET · 30 topics
Microsoft TechnologiesXAML Study Notes
.NET · 30 topics
Microsoft TechnologiesWPF (Windows Presentation Foundation) Study Notes
.NET · 30 topics
Microsoft TechnologiesMVVM Design Pattern Study Notes
.NET · 30 topics