HOWTO · Batch

The FOR Loop in Batch Scripts

Use the Windows cmd.exe FOR command in batch files to iterate over lists, number ranges, directories, files, parsed text, and command-line arguments.

On this page

In a .bat or .cmd file, use FOR with a double-percent loop variable such as %%I to run a command once for every item. At an interactive Command Prompt, use a single percent sign instead, for example %I. This distinction is the most common reason a copied FOR command fails.

FOR can iterate over a literal list, a numeric range, directories, files, parsed text, or the arguments passed to a script. The examples below use cmd.exe syntax and keep their output intentionally small so you can adapt one form at a time.

Iterate Over a List or Matching Files

Put the loop variable after FOR, the items after IN, and the command after DO. Quote a file path when it can contain spaces; use %%~fI when a command needs the loop item’s fully qualified path.

@echo off
FOR %%I IN (red green blue) DO echo %%I

Output:

red
green
blue

Replace the literal list with a wildcard such as ("*.txt") to process matching files in the current directory. The parentheses make each matching path one item, which is safer when file names contain spaces.

Count With FOR /L

FOR /L uses (start,step,end). The end value is included when the step can reach it. For a descending range, make the step negative; a positive step cannot count down.

@echo off
FOR /L %%I IN (5,-2,1) DO echo %%I

Output:

5
3
1

This form is useful for retries, numbered files, and any task that has a predictable numeric sequence. Check the direction and the sign of the step before assuming that a loop will run.

Enumerate Directories and Files

Use FOR /D for directories that match a pattern. Use FOR /R root to search a root directory and its subdirectories for files. The %%~nx modifier prints only a matched item’s name and extension, which makes the sample output independent of the current absolute path.

@echo off
FOR /D %%D IN (demo\*) DO echo DIR %%~nxD
FOR /R demo %%F IN (*.txt) DO echo FILE %%~nxF

Output:

DIR one
DIR two
FILE first.txt
FILE second.txt

FOR /R searches recursively, so a broad wildcard can touch more files than expected. Start with echo while checking the matched names, then replace it with the command that changes files.

Parse Text With FOR /F

FOR /F reads lines from a file, a quoted string, or the output of another command. Its default delimiters are spaces and tabs, so specify tokens and delims when the input has a known separator. Here, the first two comma-separated fields become %%A and %%B.

@echo off
FOR /F "tokens=1,2 delims=," %%A IN (names.txt) DO echo %%A %%B

Output:

Ada Lovelace
Grace Hopper

Do not use a made-up criterion such as "age=23" as a FOR /F option. Test the parsing clause with representative input, and remember that blank lines are skipped by default.

Process Arguments and Use Nested Loops

%* represents all arguments passed to the batch file. A nested FOR loop can combine two sets, but use a different loop variable at each level. If you change an environment variable inside parenthesized code, enable delayed expansion and read that value with !name! rather than %name%.

@echo off
FOR %%A IN (%*) DO echo ARG %%A
FOR %%I IN (1 2) DO FOR %%J IN (A B) DO echo PAIR %%I-%%J

Output when run as example.bat first second:

ARG first
ARG second
PAIR 1-A
PAIR 1-B
PAIR 2-A
PAIR 2-B

Batch files are a practical choice for small Windows automation tasks. For complex data structures, robust error handling, or cross-platform work, PowerShell is usually easier to maintain. In either tool, begin with a harmless echo loop before using commands that rename, copy, or delete files.