Home > Commands A-M > Batch F

FOR /F


Description | Syntax | Parameters | Switches | Related | Notes | Examples | Errorlevels | Availability

Conditionally process the output of a command, items in text file(s), or a text string.

FOR /F processing of a command/text-file consists of reading the output from the command/text-file 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/line 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".


Syntax

FOR /F /?

To use FOR in a batch program:

FOR /F ["[delims=xxx] [skip=n] [eol=;] [tokens=n] [usebackq]"] %%variable IN ('command_to_process' | filename | "Text string to process") DO command [command-parameters]

To use FOR from the command prompt:

FOR /F ["[delims=xxx] [skip=n] [eol=;] [tokens=n] [usebackq]"] %variable IN ('command_to_process' | filename | "Text string to process") DO command [command-parameters]


Parameters
command (NT4)
Specifies the command that you want to carry out on each file included in the specified set.
delims=xxx (NT4)
The delimiter character(s), default = a space.
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.
For example, if you need to include BOTH tab and space as delimiters, the order is significant. "delims=<tab><space>" works, "delims=<space><tab>" doesn't.
When editing a CMD script notice that many text editors will fail to enter the TAB character correctly.
You can use any character as a delimiter - but they are case sensitive.
eol=; (NT4)
Character to indicate a comment, end of line.
command-parameters (NT4)
Specifies any parameters or switches that you want to use with the specified command (if the specified command uses any parameters or switches).
('command_to_process') (NT4)
The output of the 'command_to_process' is passed into the FOR parameter.
(filename) (NT4)
A set of one or more files. Wildcards may be used. If (filename) is a period character (.) then FOR will loop through every file in the folder.
("Text string to process") (NT4)
A string of text will be treated just like a single line of input from a file, the string must be enclosed in double quotes.
skip=n (NT4)
A number of lines to skip at the beginning, default = 0.
tokens=n (NT4)
Specifies which numbered items to read from each line, default = 1.
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.
Specifying more than 1 token will cause additional parameter letters 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.
usebackq (NT2000)
Specifies that an alternative set of FOR command delimiters are to be used:
%variable or %%variable (NT4)
Represents a replaceable variable. The FOR command replaces %%variable (or %variable) with each text string in the specified set until the command (specified in the command parameter) processes all the files. Use %%variable to carry out the FOR command within a batch program. Use %variable to carry out FOR from the command prompt. (Do NOT use %0 through %9 to avoid confusion with the command-line arguments passed to the batch file).

Switches
/? (NTXP)
Display help.

Related

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.
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:
cut - Divide a file into several columns.
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.


Notes

none.


Examples

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

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

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

    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

(The long filename surrounded by "quotes" have to be escaped using ^).


Echo the dollar amount and the date:

    FOR /F "tokens=1,3* delims=," %%G IN ("12-AUG-99,deposit,$45.50,23.7") DO @ ECHO %%H was paid to %%G

Echo the month and transaction type:

    FOR /F "tokens=2,4* delims=," %%G IN ("12-AUG-99,deposit,$45.50,23.7") DO @ ECHO %%H was made in %%G

Parse each line in myfile.txt, ignoring lines that begin with a semicolon, with tokens delimited by a comma:

    FOR /F "tokens=1,3* delims=," %%G IN (myfile.txt) DO @ ECHO %%G %%H %%I

myfile.txt
[
12-AUG-99,DEPOSIT,450,23,55
; start of the new year
14-JAN-00,WITHDRAWAL,285,122
03-FEB-00,DEPOSIT,200
]

%%G = token1 = "12-AUG-99"
%%H = token3 = "450"
%%I = tokens 4+ = "23,55"

%%G is explicitly declared in the FOR statement and the %%H and %%I are implicitly declared via the tokens= option. You can specify up to 26 tokens via the tokens= line, provided this does not cause an attempt to declare a parameter higher than the letter 'Z'.

FOR parameter names are global, so in complex scripts which call one FOR statement from within another FOR statement you can refer to both sets of parameters. You cannot have more than 26 parameters active at any one time.


Errorlevels

none.


Availability
Internal
DOS
none
Windows
none
Windows NT
NT4 NT2000 NTXP NT2003