FOR

Iterates for each file in a group, for each value in a range or after tokenizing a file, string or command group.

Runs the specified command for each file in a group of files:

FOR [/D | /R [[drive:]path]] {%variable | %%variable} IN (group) DO command [parameters]

Executes the specified command for each number in a range:

FOR /L {%variable | %%variable} IN (start,increment,end) DO command [parameters]

Iterates the specified command after tokenizing a file group, string, or command:

FOR /F ["options"] {%variable | %%variable} IN ({filegroup | "string" | 'some command'}) DO command [parameters]

Where:

  • %variable - Specifies a single-letter replaceable parameter (if FOR is run directly from the command prompt).
  • %%variable - Specifies a single-letter replaceable parameter (if FOR is run from a batch filebatch file).
  • (group) - Indicates a group of one or more files (in round brackets ( )). A common wildcard is * (see WildcardsWildcards).
  • command - Specifies the command to execute for each file.
  • parameters - Indicates the parameters or options of the specified command.
  • /D - Specifies that if the group contains wildcards, directory names are used instead of file names.
  • /R [[drive:]path] - Executes FOR on each directory in the specified directory's directory structure. If no directory is specified, the current directory is used. If the group contains only a period (.), all directories in the directory structure are listed.
  • /L - Specifies in round brackets, instead of a group of files, a sequence of numbers. Examples: (1,1,4) generates 1 2 3 4, while (7,-2,1) generates 7 5 3 1.
  • /F - Instead of groups of files:
    • With filegroup a file or more files are specified. Each file is opened, read, and processed, subdividing each line of text separatedly (the subdivisions are tokens).
    • With "string" a string is specified instead of the file group.
    • With 'some command' the command is specified instead of filegroup.
    NB: It is essential to put one of these parameters together with /F, it is not possible to put two at the same time or more.
  • "options" - Changes the token subdivision. The keywords are:
    • EOL=c - Ignore lines starting with the specified character (in order to ignore comments).
    • SKIP=n - Specifies the number of lines from the beginning of the file to be ignored.
    • DELIMS=xxx - Specifies a set of comma-separated delimiters. This set replaces the default delimiters (spaces and tabs). This option must be placed last.
    • TOKENS=x,y,m-n - Specifies which tokens should be copied to which variable. x specifies a token that will be copied from the variable. y specifies a token that will be copied to the variable with the letter following the one specified. m-n specifies tokens from number m to number n that will be copied to the variable with the letter second after the one specified. Or an asterisk * is used to indicate all tokens. A maximum of 26 tokens can be specified.
    • USEBACKQ - Specifies that the new modes are active, where a back-quoted string is executed as the command and a single-quoted string is executed as the command string, allowing double-quotes for filenames in a filegroup (in short, you need this option if you put quotes around filenames when there are spaces, otherwise the system might think you want to specify multiple files or commands).

In variables, uppercase letters are different from lowercase letters (%a is different from %A). So in total there can be a maximum of 52 variables of this type. Variables can be expanded in other ways (in case you want to expand them from a batch file put an extra percent sign %). The characters between the symbols %~ and the variable character are called modifier characters (for example in the case %~fA the modifier character is f). Hint: It may be sensible to choose uppercase characters for the variable characters, to avoid confusion between modifier characters and variable characters.

  • %~A - Expands %A by removing the " " quotes.
  • %~fA - Expands %A to a fully qualified path name.
  • %~dA - Expands %A only to drive letter (file drive).
  • %~pA - Expands %A only to path (file path).
  • %~nA - Expands %A only to filename (file name).
  • %~xA - Expands %A only to ext (file extension).
  • %~sA - The expanded path contains only abbreviated names.
  • %~aA - Expands %A to attr (file attributes).
  • %~tA - Expands %A to datetime (date and time of last modified).
  • %~zA - Expands %A to size (file size).
  • %~$path:A - Searches the directories listed in the PATH environment variable and expands %A to the first fully qualified name. If the environment variable is not defined (i.e. it does not currently exist) or the file is not found in the search, expands this modifier to the first empty string.

Modifier characters can be combined together as in the following example:

%~pnA

which expands %A to path and filename only.

Examples:

1. Batch file that parses each line of the file C:\Oral-C Mortality.txt specifying that %a will have the value of the fourth token, %b the value of the twelfth token, and %c the value of the remaining tokens, ignoring lines that begin with an exclamation point !, specifying that tokens are delimited by equal signs = and spaces, and runs the command ECHO %a %b %c:

for /f "eol=! tokens=4,12* usebackq delims== " %%a in ("C:\Oral-C Mortality.txt") do echo %%a %%b %%c

Note that the variables %%b and %%c are declared implicitly thanks to the TOKENS option. Up to 26 distinct tokens associated with the corresponding letters of the alphabet can be declared in this way.

 

2. Batch file that makes a loop that repeats printing values ​​from 0 to 50 in increments of 5:

for /l %%h in (0, 5, 50) do echo %%h

 

3. Batch file that lists files in the directory C:\WINDOWS:

for %%i in ("C:\WINDOWS\*.*") do echo %%i

 

NB: the variable remains defined until the end of the execution of the FOR command.

Comments