Command Prompt for Windows

Command FOR


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
/DMatch 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=cSpecifies the end of line comment character.
skip=nSpecifies the number of line of text to skip at the beginning of the file.
delims=xxxSpecifies the delimiting characters used to separate tokens. Default delimiting characters are spaces and tabs.
tokens=n,mSpecifies 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.
usebackqAlters 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
%variableSpecifies the replaceable variable a to z or A to Z. variable is not an environment variable.
envvarSpecifies 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.
num1Number specifying the start of the range.
num2Number specifying the end of the range.
num3Number specifying the increment value. The default value is 1 when no STEP value is specified.
commandSpecifies the command to be performed for each file found in the set.

Variable Modifiers

The FOR variable can include the optional ~ modifier. The following syntax is used to specify a ~ modifier :-

~remove any surrounding double quotes (ie. ").
~areturn the file attributes.
~dreturn the drive letter only.
~freturn the fully qualified path name.
~nreturn the file name only.
~preturn the path only.
~sthe path returned contains short names only.
~treturn the date/time of file.
~xreturn the file extension only.
~zreturn 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.
variablethe 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.

Also see command DO, WHILE, UNTIL, Command Grouping and Batch Files.