text
stringlengths
0
4.23k
You can use this command within a batch file or directly from the command prompt.
The following attributes apply to the for command:
This command replaces % variable or %% variable with each text string in the specified set until the specified command processes all of the files.
% variable
%% variable
Variable names are case sensitive, global, and no more than 52 can be active at a time.
To avoid confusion with the batch parameters, %0 through %9, you can use any character for variable except the numerals 0 through 9. For simple batch files, a single character such as %%f will work.
%0
%9
%%f
You can use multiple values for variable in complex batch files to distinguish different replaceable variables.
The set parameter can represent a single group of files or several groups of files. You can use wildcard characters (* and ?) to specify a file set. The following are valid file sets:
(*.doc)
(*.doc *.txt *.me)
(jan*.doc jan*.rpt feb*.doc feb*.rpt)
(ar??1991.* ap??1991.*)
When you use this command, the first value in set replaces % variable or %% variable, and then the specified command processes this value. This continues until all of the files (or groups of files) that correspond to the set value are processed.
% variable
%% variable
In and do aren't parameters, but you must use them with this command. If you omit either of these keywords, an error message appears.
If command extensions are enabled (that is the default), the following additional forms of for are supported:
Directories only: If set contains wildcard characters (* or ?), the specified command executes for each directory (instead of a set of files in a specified directory) that matches set. The syntax is:
for /d {%%|%}<Variable> in (<Set>) do <Command> [<CommandLineOptions>]
Recursive: Walks the directory tree that is rooted at drive:path and executes the for statement in each directory of the tree. If no directory is specified after /r, the current directory is used as the root directory. If set is just a single period (.), it only enumerates the directory tree. The syntax is:
for /r [[<drive>:]<path>] {%%|%}<variable> in (<set>) do <command> [<commandlinepptions>]
Iterating a range of values: Use an iterative variable to set the starting value (start#) and then step through a set range of values until the value exceeds the set ending value (end#). /l will execute the iterative by comparing start# with end#. If start# is less than end# the command will execute. When the iterative variable exceeds end#, the command shell exits the loop. You can also use a negative step# to step through a range in decreasing values. For example, (1,1,5) generates the sequence 1 2 3 4 5 and (5,-1,1) generates the sequence 5 4 3 2 1. The syntax is:
for /l {%%|%}<variable> in (<start#>,<step#>,<end#>) do <command> [<commandlinepptions>]
Iterating and file parsing: Use file parsing to process command output, strings, and file content. Use iterative variables to define the content or strings that you want to examine, and use the various parsingkeywords options to further modify the parsing. Use the parsingkeywords token option to specify which tokens should be passed as iterative variables. When used without the token option, /f will only examine the first token.
File parsing consists of reading the output, string, or file content, and then breaking it into individual lines of text and parsing each line into zero or more tokens. The for loop is then called with the iterative variable value set to the token. By default, /f passes the first blank separated token from each line of each file. Blank lines are skipped.
The syntaxes are:
for /f [<parsingkeywords>] {%%|%}<variable> in (<set>) do <command> [<commandlinepptions>]
for /f [<parsingkeywords>] {%%|%}<variable> in (<literalstring>) do <command> [<commandlinepptions>]
for /f [<parsingkeywords>] {%%|%}<variable> in ('<command>') do <command> [<commandlinepptions>]
The set argument specifies one or more file names. Each file is opened, read, and processed before moving to the next file in set. To override the default parsing behavior, specify parsingkeywords. This is a quoted string that contains one or more keywords to specify different parsing options.
If you use the usebackq option, use one of the following syntaxes:
for /f [usebackq <parsingkeywords>] {%%|%}<variable> in (<Set>) do <command> [<commandlinepptions>]
for /f [usebackq <parsingkeywords>] {%%|%}<variable> in ('<LiteralString>') do <command> [<commandlinepptions>]
for /f [usebackq <parsingkeywords>] {%%|%}<variable> in (`<command>`) do <command> [<commandlinepptions>]
The following table lists the parsing keywords that you can use for parsingkeywords.
Keyword
Description
eol=<c>
Specifies an end of line character (just one character).
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 loop for each iteration. As a result, additional variable names are allocated. m-n specifies a range, from the mth through the nth tokens. If the last character in the tokens= string is an asterisk (*), an additional variable is allocated, and it receives the remaining text on the line after the last token that is parsed.
usebackq
Specifies to run a back-quoted string as a command, use a single-quoted string as a literal string, or, for long file names that contain spaces, allow file names in <set>, to each be enclosed in double-quotation marks.
<c>
<n>
<xxx>
<x,y,m–n>
<set>
Variable substitution: The following table lists optional syntax (for any variable I):
Variable with modifier
Description
%~I
Expands %I which removes any surrounding quotation marks.
%~fI
Expands %I to a fully qualified path name.
%~dI
Expands %I to a drive letter only.
%~pI
Expands %I to a path only.
%~nI
Expands %I to a file name only.
%~xI
Expands %I to a file name extension only.
%~sI