Windows Batch Files
Reading and maintaining .bat/.cmd scripts: variables, if, for /F, delayed expansion, labels and errorlevel.
Batch files are still everywhere on Windows: build and flash scripts shipped with toolchains, installer helpers, lab automation from years ago. The language is limited and full of quirks, so the goal here is to read and fix them confidently — and to know which quirks bite.
A tour of the language
@echo off stops cmd from printing each command before running it.
set name=value (no spaces around =) and %name% to read; set /a does integer arithmetic.
if supports string comparison (==), numbers (GTR, LSS, EQU...), exist and not.
for /L counts; for /F reads a file line by line, splitting columns with tokens= and delims=.
Inside a ( ) block, %count% would be expanded once before the loop runs; with EnableDelayedExpansion, !count! reads the current value.
call :greet Meera calls a label like a function; %~1 is its first argument without quotes; exit /b returns from it.
Quirks to know
set count=0 for... do ( set /a count+=1 echo %count%) setlocal EnableDelayedExpansion... echo !count!
%count% inside the block is replaced before the loop runs, so it always prints 0.
Why does a batch script use !var! instead of %var% inside a for loop?
%var% is expanded once when the block is parsed; with delayed expansion enabled, !var! is expanded each time the line runs, so it sees updated values.