The Basic For Loop
The simplest form, for %%X in (a b c) do echo %%X, iterates over a space-separated list, assigning each item in turn to the loop variable %%X (a single percent sign is used when typed directly at the command prompt, but scripts require the doubled %%). The list can be literal words, wildcarded filenames like *.txt, or a mix of both, and each item is substituted exactly as written, with no automatic splitting on anything other than whitespace by default. Loop variable names are case-sensitive single letters or short tokens, and %%i is the long-standing convention inherited from early DOS scripting tutorials.
Cricket analogy: Going through a list of five bowlers to check each one's economy rate in turn is like for %%B in (Bumrah Shami Siraj Chahal Kuldeep) do echo %%B, processing each named item exactly once in order.
@echo off
for %%F in (*.log) do (
echo Processing %%F
type %%F
)
Looping Over Ranges and Files with /L and /R
for /L %%N in (start,step,end) do generates an arithmetic sequence without needing an external list, so for /L %%N in (1,1,10) do echo %%N counts from 1 to 10, and a negative step like (10,-1,1) counts down. for /R walks a directory tree recursively, executing the loop body once per file found under the given root, which is the batch equivalent of a recursive file search: for /R C:\Logs %%F in (*.log) do echo %%F finds every .log file at any depth under C:\Logs. Both variants use the same %%variable substitution rules as the basic form.
Cricket analogy: Counting deliveries in an over from ball 1 to ball 6 is like for /L %%B in (1,1,6) do echo Ball %%B, generating a numeric sequence without needing to type out a literal list.
for /D %%X in (*) do echo %%X iterates only over directories in the current folder, ignoring files, which is useful for scripts that need to process every subfolder without touching loose files.
Parsing Command Output with For /F
for /F is the most powerful variant, parsing lines of text from a file, a quoted string, or the output of another command, splitting each line into tokens by delimiter (space by default). for /F "tokens=1,2 delims=," %%A in (data.csv) do echo %%A - %%B reads a comma-separated file and assigns the first token to %%A and the second to %%B automatically. Running a command's output through for /F ("usebackq delims=" %%V in (command) do ... (the backquotes trigger command execution) is the standard way to capture a program's output into a variable inside a batch script.
Cricket analogy: Parsing a CSV scorecard where each line lists player,runs,balls and pulling out just the name and runs columns is like for /F "tokens=1,2 delims=," %%P in (scorecard.csv) do echo %%P scored %%Q runs, splitting fields by a delimiter.
Inside a for /F loop body, only the first declared loop variable (%%A) is required, but subsequent tokens are assigned to consecutive letters automatically (%%B, %%C, ...), so requesting tokens=1,2,3 with only %%A declared silently drops the extra columns unless you declare enough variables.
for %%X in (list) do commanditerates a literal or wildcarded list; use %% in scripts, single % only at the interactive prompt.for /L %%N in (start,step,end) dogenerates an arithmetic sequence, including counting down with a negative step.for /R path %%F in (pattern) dorecursively walks a directory tree, running the loop body for every matching file at any depth.for /D %%X in (*) doiterates directories only, skipping files, for folder-focused operations.for /F "tokens=N delims=X" %%A in (file) doparses lines from a file, splitting on a delimiter into token variables.- Wrapping a command in backquotes inside a for /F loop, like ('command'), captures that command's live output for parsing.
- Declared loop variables auto-increment through consecutive letters (%%A, %%B, %%C) to receive multiple requested tokens.
Practice what you learned
1. In a .bat script file, how should a for loop variable be written?
2. What does `for /L %%N in (10,-1,1) do echo %%N` produce?
3. What is the purpose of `for /R` compared to the basic `for` loop?
4. In `for /F "tokens=1,2 delims=," %%A in (data.csv) do echo %%A %%B`, where does the second token's value get stored?
5. How would you capture the output of the command `whoami` into a variable using for /F?
Was this page helpful?
You May Also Like
Variables in Batch Scripts
Learn how Windows batch scripts store, read, and manipulate values using the set command, environment variables, delayed expansion, and substring syntax.
If/Else in Batch
Master conditional logic in Windows batch files: comparing strings and numbers, testing file existence, checking errorlevel, and chaining else branches.
Goto and Labels
Understand how batch scripts use labels and the goto command to jump execution, build subroutines with call, and structure menu-driven scripts.
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