100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Batch

Batch Script Quick Reference

A condensed lookup of essential Windows batch syntax — variables, control flow, string operations, and exit codes — for day-to-day scripting.

Practical Batch ScriptingBeginner8 min readJul 10, 2026
Analogies

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.

batch
@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

Was this page helpful?

Topics covered

#Batch#WindowsBatchScriptingStudyNotes#MicrosoftTechnologies#BatchScriptQuickReference#Script#Quick#Reference#Variables#StudyNotes#SkillVeris