Execute a specific command for each file in a group of files.
FOR %variable IN (set) DO command [command-parameters]
%variable specifies a single alphabetic replaceable parameter.
(set) specifies one or a group of files. Wildcards can be used.
command specifies the command to be executed for each file.
command-parameters specifies parameters or command-line switches for the specific command.
When using the FOR command in a batch file, specify the variable using %%variable instead of %variable. Variable names are case-sensitive, so %i is different from %I.
If command extensions are enabled, the following additional FOR command formats are supported:
FOR /D %variable IN (set) DO command [command-parameters]
If wildcards are included in the set, it specifies matching directory names instead of file names.
FOR /R [[drive:]path] %variable IN (set) DO command [command-parameters]
Checks the directory tree rooted at [drive:]path, directing the FOR statement to each directory. If no directory is specified after /R, the current directory is used. If the set is only a single dot (.), the directory tree is enumerated.
FOR /L %variable IN (start,step,end) DO command [command-parameters]
The set represents a sequence of numbers from start to end in increments. Thus, (1,1,5) will generate the sequence 1 2 3 4 5, and (5,-1,1) will generate the sequence (5 4 3 2 1).
FOR /F ["options"] %variable IN (file-set) DO command [command-parameters]
FOR /F ["options"] %variable IN ("string") DO command [command-parameters]
FOR /F ["options"] %variable IN ('command') DO command [command-parameters]
Or, if the usebackq option is used:
FOR /F ["options"] %variable IN (file-set) DO command [command-parameters]
FOR /F ["options"] %variable IN ("string") DO command [command-parameters]
FOR /F ["options"] %variable IN ('command') DO command [command-parameters]
filenameset is one or more filenames. Each file is opened, read, and processed before continuing to the next file in filenameset. Processing includes reading the file, dividing it into lines of text, and then parsing each line into zero or more tokens. The For loop is then called with the found token string variable values. By default, /F separates by the first blank token in each line of each file. Blank lines are skipped. You can substitute the default parsing operation by specifying the optional "options" parameter. This quoted string includes one or more keywords specifying different parsing options. These keywords are:
eol=c - specifies the end of a line comment character (just one)
skip=n - specifies the number of lines to skip at the beginning of the file.
delims=xxx - specifies the delimiter set. This replaces the default delimiter set of space and tab.
tokens=x,y,m-n - specifies which tokens of each line are passed to each iteration of the for itself. This causes the assignment of additional variable names. The m-n format is a range. Specifies the mth through the nth token. If the last character in the token string is an asterisk, then additional variables are assigned and receive the remaining text of the line after the last token parsed.
usebackq - specifies that the new syntax is used in the following cases:
executing a backquoted string as a command and a single quote character as a literal string command and allowing file names to be enclosed in double quotes in filenameset.
Some examples may be helpful:
FOR /F "eol=; tokens=2,3* delims=, " %i in (myfile.txt) do @echo %i %j %k
will parse each line in myfile.txt, skipping lines that begin with a semicolon, passing the second and third tokens of each line to the for body; delimited by commas and/or spaces. Note that the for body statement references %i to get the second token, %j to get the third token, and %k to get all remaining tokens after the third token. For filenames with spaces, you need to enclose the filename in double quotes. To use double quotes in this way, you also need to use the usebackq option, otherwise, the double quotes will be interpreted as defining a string to be parsed.
%i is specifically explained in the for statement, and %j and %k are specifically explained by the tokens= option. You can specify up to 26 tokens in a tokens= line, as long as you do not attempt to define a variable higher than 'z' or 'Z'. Remember that FOR variables are single alphabetic, case-sensitive, and global; and, there can be no more than 52 in use at the same time.
You can also use the FOR /F parsing logic on adjacent strings; by enclosing the filenameset between parentheses in single quotes. Thus, the string is treated as a single input line in a file.
Finally, you can use the FOR /F command to parse the output of a command. By making the filenameset between parentheses a backquoted string. The string is treated as a command line, passed to a sub CMD.EXE, whose output is captured in memory and treated as a file to parse. Thus, the following example:
FOR /F "usebackq delims==" %i IN (`set`) DO @echo %i
will enumerate the names of environment variables in the current environment.
In addition, substitution of FOR variable references has been enhanced. You can now use the following option syntax:
~I - removes any quotes (") and expands %I
%~fI - expands %I to a fully qualified path name
%~dI - expands %I to just a drive letter
%~pI - expands %I to just a path
%~nI - expands %I to just a file name
%~xI - expands %I to just a file extension
%~sI - expanded path contains only short names
%~aI - expands %I to the file attributes of the file
%~tI - expands %I to the date/time of the file
%~zI - expands %I to the size of the file
%~$PATH:I - searches the directories listed in the path environment variable and expands %I to the first fully qualified name found. If the environment variable name is not defined or the file is not found, this combination expands to an empty string
You can combine modifiers to get multiple results:
%~dpI - expands %I to just a drive letter and path
%~nxI - expands %I to just a file name and extension
%~fsI - expands %I to a full path name with short names only
%~dp$PATH:i - searches the directories listed in the path environment variable and expands %I to the first drive letter and path found.
%~ftzaI - expands %I to a DIR-like output line
In the above examples, %I and PATH can be replaced with other valid values. The %~ syntax terminates with a valid FOR variable name. Choosing an uppercase variable name like %I is more readable and avoids confusion with case-insensitive combinations.