Home NT Commands
NT Syntax

FOR /F

Conditionally process the output of a command.

syntax
      FOR /F ["options"] %%parameter IN ('command_to_process') DO command

key
   options:
           delims=xxx  - The delimiter character(s)
                         (default = a space)
           skip=n      - A number of lines to skip at the beginning. 
                         (default = 0)

eol=; - character to indicate a comment (end of line)
tokens=n - specifies which numbered items to read from each line (default = 1) usebackq - under Windows 2000 (and greater) the usebackq option specifies that an alternative set of FOR command delimiters are to be used: - a `command_to_process` is placed in BACK quotes instead of 'straight' quotes (see the FOR /F filename syntax for more) command_to_process : The output of the 'command_to_process' is passed into the FOR parameter. command : The command to carry out, including any command-line parameters. %%parameter : A replaceable parameter: in a batch file use %%G (on the command line %G)

FOR /F processing of a command consists of reading the output from the command one line at a time and then breaking the line up into individual items of data or 'tokens'. The DO command is then executed with the parameter(s) set to the token(s) found.

By default, /F breaks up the command output at each blank space, and any blank lines are skipped.
You can override this default parsing behavior by specifying the "options" parameter. The options must be contained within "quotes"

Tokens
tokens=2,4,6 will cause the second, fourth and sixth items on each line to be processed

tokens=2-6 will cause the second, third, fourth, fifth and sixth items on each line to be processed

tokens=* will cause all items on each line to be processed

tokens=3* will cause the 3rd and all subsequent items on each line to be processed

Each token specified will cause a corresponding parameter letter to be allocated.

If the last character in the tokens= string is an asterisk, then additional parameters are allocated for all the remaining text on the line.

Delims
Specifying more than one delimiter has been known to cause problems with some data sets, if you have problems try parsing with just one delimiter at a time, or change the order in which they are listed.

You can use any character as a delimiter - but they are case sensitive.

Examples:

To ECHO from the command line, the name of every environment variable.

   FOR /F "delims==" %G IN ('SET') DO @ECHO %G

The same command with usebackq (Windows 2000 only)

   FOR /F "usebackq delims==" %G IN (`SET`) DO @ECHO %G

To put the Windows Version into an environment variable

   @echo off 
   ::parse the VER command 
   FOR /F "tokens=4*" %%G IN ('ver') DO SET v_version=%%G 
   :: show the result 
   echo %v_version%

List all the text files in a folder

   FOR /F "tokens=*" %%G IN ('dir/b ^"c:\program files\*.txt^"') DO echo %%G

In the example above the long filename has to be surrounded in "quotes"
these quotes have to be escaped using ^
The "tokens=*" has been added to match all parts of any long filenames returned by the DIR command.

Although the above is a trivial example, being able to set %%G equal to each long filename in turn could allow much more complex processing to be done.

Related Commands:

FOR
- Loop commands

FOR - Loop through a set of files in one folder
FOR /R - Loop through files (recurse subfolders)
FOR /D
- Loop through several folders
FOR /L - Loop through a range of numbers
FOR /F - Loop through items in a text file

FORFILES - Batch process multiple files
GOTO - Direct a batch program to jump to a labelled line
IF - Conditionally perform a command

Equivalent Linux BASH commands:

for - Expand words, and execute commands
case - Conditionally perform a command
eval - Evaluate several commands/arguments
if - Conditionally perform a command
gawk - Find and Replace text within file(s)
m4 - Macro processor
until - Execute commands (until error)
while - Execute commands



Simon Sheppard
SS64.com