Runs a specified command for each file in a set of files.
|FOR||%variable IN (set) DO command [command-parameters]|
|%variable||Specifies a replaceable parameter.|
|(set)||Specifies a set of one or more files. Wildcards may be used.|
|command||Specifies the command to carry out for each file.|
|command-parameters||Specifies parameters or switches for the specified command.|
To use the FOR command in a batch program, specify
%%variable instead of
Variable names are case sensitive, so
%i is different from
If Command Extensions are enabled, the following additional forms of the FOR command are supported:
|FOR||/D %variable IN (set) DO command [command-parameters]|
If set contains wildcards, then specifies to match against directory names instead of file names.
|FOR||/R [[drive:]path] %variable IN (set) DO command [command-parameters]|
Walks the directory tree rooted at
[drive:]path, executing the
FOR statement in each directory of the tree.
If no directory specification is specified after
/R then the current directory is assumed.
If set is just a single period (.) character then it will just enumerate the directory tree.
|FOR||/L %variable IN (start,step,end) DO command [command-parameters]|
The set is a sequence of numbers from
(1,1,5) would generate the sequence
1 2 3 4 5 and
(5,-1,1) would generate the sequence
5 4 3 2 1.
This is the batch equivalent of
Breaking a continuous loop cannot be done programmatically, you will need to press
Breaking an "almost continuous" loop is easier, using
However, keep in mind that in a
This means the code line above may take an hour or more to finish.
If you need loops that can and will be broken, better use Do While or Do Until loops, even thought they aren't very readable in batch (but then, what is?).
|FOR||/F ["options"] %variable IN (filenameset) DO command [command-parameters]|
|FOR||/F ["options"] %variable IN ("string") DO command [command-parameters]|
|FOR||/F ["options"] %variable IN ('command') DO command [command-parameters]|
usebackq (not available in Windows NT 4, requires Windows 2000 or later):
|FOR||/F ["usebackq options"] %variable IN (filenameset) DO command [command-parameters]|
|FOR||/F ["usebackq options"] %variable IN ('string') DO command [command-parameters]|
|FOR||/F ["usebackq options"] %variable IN (`command`) DO command [command-parameters]|
filenameset is one or more file names.
Each file is opened, read and processed before going on to the next file in
Processing consists of reading in the file, breaking it up into individual lines of text and then parsing each line into zero or more tokens. The body of the for loop is then called with the variable value(s) set to the found token string(s).
/F passes the first blank separated token from each line of each file.
Blank lines are skipped.
You can override the default parsing behavior by specifying the optional
This is a quoted string which contains one or more keywords to specify different parsing parameters.
The keywords are:
|eol=c||-||specifies an end of line comment character (just one).
|skip=n||-||specifies the number of lines to skip at the beginning of the file.|
|delims=xxx||-||specifies a delimiter set. This replaces the default delimiter set of space and tab.|
|tokens=x,y,m-n||-||specifies which tokens from each line are to be passed to the for body for each iteration.
This will cause additional variable names to be allocated.
The m-n form is a range, specifying the mth through the nth tokens.
If the last character in the
|usebackq||-||(not available in Windows NT 4) specifies that the new semantics are in force, where a back quoted string is executed as a command and a single quoted string is a literal string command and allows the use of double quotes to quote file names in filenameset.|
Some examples might help:
|FOR||/F "eol=; tokens=2,3* delims=, " %i IN (myfile.txt) DO @ECHO %i %j %k|
would parse each line in myfile.txt, ignoring lines that begin with a semicolon, passing the 2nd and 3rd token from each line to the for body, with tokens delimited by commas and/or spaces.
Notice the for body statements reference
%i to get the 2nd token,
%j to get the 3rd token, and
%k to get all remaining tokens after the 3rd.
For file names that contain spaces, you need to quote the filenames with double quotes.
In order to use double quotes in this manner, you also need to use the usebackq option (not available in Windows NT 4), otherwise the double quotes will be interpreted as defining a literal string to parse.
%i is explicitly declared in the for statement and the
%k are implicitly declared via the
You can specify up to 26 tokens via the
tokens= line, provided it does not cause an attempt to declare a variable higher than the letter 'z'.
FOR variable names are global, and you can't have more than 52 (26 for NT 4) total active at any one time.
You can also use the
FOR /F parsing logic on an immediate string, by making the filenameset between the parenthesis a quoted string.
It will be treated as a single line of input from a file and parsed.
Finally, you can use the
FOR /F command to parse the output of a command.
You do this by making the filenameset between the parenthesis a single quoted (Windows NT 4 and later) or back quoted (Windows 2000 and later) string.
It will be treated as a command line, which is passed to a child CMD.EXE and the output is captured into memory and parsed as if it was a file.
So the following examples:
|FOR||/F "delims==" %i IN ('SET') DO @ECHO %i|
|FOR||/F "usebackq delims==" %i IN (`SET`) DO @ECHO %i|
would enumerate the environment variable names in the current environment by parsing the output of the
In addition, substitution of FOR variable references has been enhanced.
You can now use the following optional syntax:
|%~si||-||expanded path contains short names only|
|%~$PATH:i||-||searches the directories listed in the
If the environment variable name is not defined or the file is not found by the search, then this modifier expands to the empty string
The modifiers can be combined to get compound results:
|%~dp$PATH:i||-||searches the directories listed in the
In the above examples
PATH can be replaced by other valid values.
Just be careful to pick your
FOR variable letters to not conflict with any of the format specifier letters if you plan on using the enhanced substitution logic.
%~ syntax is terminated by a valid
FOR variable name.
Picking upper case variable names like
%I makes it more readable and avoids confusion with the modifiers, which are not case sensitive.
page last modified: 2022-03-09
SETLOCAL ENABLEEXTENSIONSwithin your NT shell scripts (batch files) or execute those scripts using
SETLOCALpage if you intend to use