Perform a command for each file in the specified set of files or perform a command a given number of times.
FOR [/D] [/R [path]] [/F ["options"]] %variable IN ( set ) DO command
FOR [/R [path]] %variable IS num1 TO num2 [STEP num3] DO command
FOR [/R [path]] envvar IS num1 TO num2 [STEP num3] DO command
/D Match directory names instead of file names. /F ["options"] Parse a text file and return the tokens found. The "options" string contains any combination of the following keywords :-
eol=c Specifies the end of line comment character. skip=n Specifies the number of line of text to skip at the beginning of the file. delims=xxx Specifies the delimiting characters used to separate tokens. Default delimiting characters are spaces and tabs. tokens=n,m Specifies which token(s) to return. When more then one token is required, separate the list of numbers with commas. A convenient shorthand to specify a range of tokens is n-m. Include a * at the end of the token= string to return the remaining characters after the last token requested is returned. usebackq Alters the symantics of (set). See notes. /R [path] Execute the FOR command for each directory of the tree starting from path. If path is not specified, start from the current directory %variable Specifies the replaceable variable a to z or A to Z. variable is not an environment variable. envvar Specifies the environment variable to use. Do not (set) Specifies a set of files. Wildcards can be used and file names can be separated by commas. num1 Number specifying the start of the range. num2 Number specifying the end of the range. num3 Number specifying the increment value. The default value is 1 when no STEP value is specified. command Specifies the command to be performed for each file found in the set.
The FOR variable can include the optional ~ modifier. The following syntax is used to specify a ~ modifier :-
~ remove any surrounding double quotes (ie. "). ~a return the file attributes. ~d return the drive letter only. ~f return the fully qualified path name. ~n return the file name only. ~p return the path only. ~s the path returned contains short names only. ~t return the date/time of file. ~x return the file extension only. ~z return the size of file. ~$env: search the directories listed in the environment variable and return the fully qualified name of the first one found. If the environment variable name is not defined or the file is not found by the search, then an empty string is returned. variable the FOR variable a to z or A to Z.
There is no particular order the modifiers need to be specified. env can be any valid environment variable, such as the PATH environment variable.
When using the FOR command inside a batch file, use %%variable and not %variable.
Variable is case sensitive, that is, %a is not the same variable as %A.
When specifying an environment variable, do not include the %s around the envvar.
The parameters num1, num2 and num3 may either be specified as integer or real numbers.
When the /F switch is used the (set) can include filenames, strings or commands :-
( file1, file2, ... ) - a set of files to parse ( "str1", "str2", ... ) - a set of strings to parse using double quotes ( 'cmd1', 'cmd2', ... ) - a set of commands to execute and parse the output.
Specifying usebackq for the “options”, (set) changes symantics as follows :-
( file1, file2, ... ) - a set of files to parse ( 'str1', 'str2', ... ) - a set of strings to parse using single quotes ( `cmd1`, `cmd2`, ... ) - a set of commands to execute and parse the output.