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

For Loops in Batch

Learn the many forms of the batch for command: iterating lists, ranges, files, directories, and command output with %%variables.

Variables and Control FlowIntermediate11 min readJul 10, 2026
Analogies

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.

batch
@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 command iterates a literal or wildcarded list; use %% in scripts, single % only at the interactive prompt.
  • for /L %%N in (start,step,end) do generates an arithmetic sequence, including counting down with a negative step.
  • for /R path %%F in (pattern) do recursively walks a directory tree, running the loop body for every matching file at any depth.
  • for /D %%X in (*) do iterates directories only, skipping files, for folder-focused operations.
  • for /F "tokens=N delims=X" %%A in (file) do parses 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

Was this page helpful?

Topics covered

#Batch#WindowsBatchScriptingStudyNotes#MicrosoftTechnologies#ForLoopsInBatch#Loops#Loop#Looping#Over#StudyNotes#SkillVeris