- SS64
- CMD
- How-to
Loop command: against a set of files - conditionally perform a command against each item.
Syntax FOR /F ["options"] %%parameter IN (filenameset) DO command FOR /F ["options"] %%parameter IN ("Text string to process") DO commandKey options: delims=xxx The delimiter character(s). Default for strings = a space or TAB. skip=n A number of lines to skip at the beginning of the file. (default = 0) eol=; Character at the start of each line to indicate a comment The default is a semicolon ; tokens=n Specifies which numbered items to read from each line (default = 1) usebackq Use the alternate quoting style: - Instead of double quotes use single quotes for 'Text string to process' - Use double quotes for long file names in "filenameset". - Use back quotes for `command to process` Filenameset A set of one or more text files, the contents of which will be parsed. Enclose the list of files in parentheses (file1.txt,file2.txt). command The command to carry out, including any parameters. This can be a single command, or if you enclose it in (parentheses), several commands, one per line. %%parameter A replaceable parameter: in a batch file use %%G (on the command line %G)
FOR /F processing of a text file consists of reading the file, one line of text at a time and then breaking the line up into individual items of data called 'tokens'. The DO command is then executed with the parameter(s) set to the token(s) found.
If you want to process the File Names rather than the content of the files, use the FOR /F command_to_process variant, with DIR /b as the command.
FOR parameters (%%A – %%Z)
Read the main FOR introduction page for a full description of assigning the replaceable %%parameter.
FOR parameters are used in all variations of the FOR command, it is a good idea to get up to speed with writing a basic FOR command before trying the more complex FOR / F variant.
By default, /F breaks up each line within the file at each blank space " ", and any blank lines are skipped, this default parsing behavior can be changed by applying one or more of the "options" parameters. The option(s) must be contained "within quotes"
Within a FOR loop the visibility of variables is affected by SETLOCAL EnableDelayedExpansion, by default variable changes within the loop will not be visible until the loop completes.
usebackq
This option is useful when dealing with a filenameset that is a long filename containing spaces, it allows you to put double quotes around the filename.
Because double quotes are used to indicate a filename, it then uses single quotes instead of double quotes to indicate a text string and backquotes for a command to process. The backquote character ` is just below the ESC key on most keyboards.
Filenames which don’t contan spaces can still be referenced without using quotes.Usebackq can be abbreviated to useback (undocumented.)
Skip
SKIP will skip processing a number of lines from the beginning of the file.
SKIP includes empty lines, but after the SKIP is complete, FOR /F ignores (does not iterate) empty lines.
eol
The default end-of-line character is a semicolon ';' when the FOR command reads a text file (or even a character string), any line that STARTS with the eol character will be ignored. In other words it is treated as a comment.
Use eol=X to change the eol character to X.Often you will want to turn this feature off so that every line of your data file is processed, in theory "eol=" should turn this feature off, but in practice this fails to work correctly - it will set eol to whatever the next character is, often the quote or space character. One workaround is to set eol to some unusual character that you don’t expect to ever encounter in the data file e.g. "eol=€" or "eol=¬". Another method is to escape every delimiter For /f tokens^=*^ delims^=^ eol^= %%a in (file.txt) do... (see forum for a discussion of this)
None of the options can be repeated, if you include say"eol=# eol=@" then only the second, "eol=@" is applied.
Delims
More than one delimiter can be specified so a string like 'abcd;efg+hijk;lmno;pqr' can be broken up using "delims=;+".
for /f "tokens=1,2,3,4,5 delims=;+" %%G in (filename.txt) do echo %%G %%H %%K
For simple FOR commands which only process one item at a time, use "delims="
This will place everything on the line into the first token. even if it contains spaces, e.g. a long filename.for /f "usebackq delims=" %%G in ("file 1.txt","file 2.txt","file 3.txt") do echo %%G
See Also6.2. Feature extractionExtract function - RDocumentation8.3: EFA Steps with Factor ExtractionYou can use almost any character as a delimiter, but they are case sensitive.
delims should always the last item in the options string "tokens=3 delims= " not "delims= tokens=3"
This is because the quotations around the options string do double duty as a terminator for the delims character(s), which is particularly important when that character is a space.If you don’t specify delims it will default to "delims=<TAB><Space>"
This is different from the default delimiters for the CMD shell command line (Space,Tab,Comma,Equals,Semicolon). All of those can be specified as delimiters with the FOR command, but when using = as a delimiter it cannot be the last one in the list. So this is valid: "delims=;= " and this is not valid: "delims=; ="Consecutive delimiters will be treated as one, even if they are different characters.
This means that if any data values are missing between delimiters the subsequent items in that line will be assigned a different token, e.g.Bread,Organic,$1.49
Cheese,,$2.99
In the first line the price will be assigned to the third token, but for the second line, the price will be assigned to the second token.
A common workaround for this issue is to pre-process the input data adding a placeholder character between any consecutive delimiters. e.g. replace ',,' with ',~,'One special case is using a double quote (") as delimiter.
By default this will be misinterpreted as the end of the "delims string", a workaround for this is to remove the outer enclosing quotes and instead escape all the delimiter chars with ^.for /f tokens^=1^,2^,3^ delims^=^" %%G in (filename.txt) do echo %%G %%H %%I
n.b. some text editors will enter the TAB character as a series of spaces.
Tokens
tokens=2,4,6 Will cause the second, fourth and sixth items on each line to be processed (3 Tokens).
tokens=2-6 Will cause the second, third, fourth, fifth and sixth items on each line to be processed (5 Tokens).
tokens=* Will cause all items on each line to be processed as a single string Token.
tokens=3,* Will process the third item as one string token and then all subsequent text as a second string Token.
This can also be written as tokens=3*If the last character in the tokens= string is an asterisk '*', then one additional parameter is added for all the remaining text on the line.
If an asterix is used in the tokens= string then it must be the last character in the string.
The parameter added by the asterix is based on the whole tokens= string and not on the number preceding the asterix, so if you select tokens 1,2,* the asterisk will represent the third and all subsequent items, and if you select tokens=19,2,1,* then the asterisk will represent the 20th and all subsequent items.
Each token specified will cause a corresponding parameter letter to be allocated. The letters used for tokens are case sensitive.
The following ASCII characters can be used as FOR tokens:
ASCII 63 - 93 inclusive, 31 tokens: ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ]
ASCII 95-123 inclusive, 29 tokens: _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z {
(there are a few other characters that can be used, but require escaping)A single FOR /F command can never parse more than 31 tokens, to use more requires a workaround with multiple FOR commands.
The tokens selected do not have to be listed in numeric order, though it is usually more readable to do so. The numbers selected in tokens= are automatically sorted, so for example tokens=5,7,1-3 and tokens=1,2,3,5,7 both produce the same result.
Matching the same token more than once (tokens=1,2,1) can give unpredictable results. Token variables can of course be used multiple times: Echo %%G %%H %%G
FOR tokens variables (or 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.
Bugs: When using tokens=* (just an asterisk, and no other tokens) against a string which contains one or more delimiter characters and no other text, then the commands will be executed despite no text matching the token having been found, an empty parameter is generated. More complex patterns like tokens=1,* do not behave this way.
The precedence/priority of FOR command options is: usebackq > skip > delims > eol > tokens
Examples
Select items from a string and display them:
@Echo Off FOR /f "tokens=1,2,3,*" %%A in ("alpha beta gamma delta epsilon") DO echo A:[%%A] B:[%%B] C:[%%C] D:[%%D]
Copy the filenames listed in a text file (one per line) to a new destination:
FOR /f "delims=" %%G in (files.txt) DO copy "\\source\folder\%%G" "H:\destination\%%G"
Extract data from a text file which contains characters and commas (but no spaces or other punctuation):
January,Snowy,02
February,Rainy,15
March,Sunny,25FOR /F "tokens=1,3 delims=," %%G IN (weather.txt) DO @echo %%G %%H
The tricky part is splitting up each the line into the right tokens, in this case I’m splitting on the comma character ',' this splits the line into 3 chunks of text and we pull out the first and third items with "tokens=1,3"
token=1 (%%G) token=2 token=3 (%%H) (ignored) January 02 February 15 March 25 %%G is declared in the FOR statement and %%H is implicitly declared via the tokens= option.
An alternative way to retrieve the same data would be:
FOR /F "tokens=1,2,3 delims=," %%G IN (weather.txt) DO @echo %%G %%I
Split a string at each space.
This can be done just as above, replacing "delims=," with "delims= "
It is possible to specify either the tokens= and/or delims= options in any order, but whenever both delims and tokens are specified, they must be separated by a space, this space will NOT count as a token. For this reason it is recommended to always place delims as the last option before the closing quotation, it is much easier to see what is happening with one space (or no spaces) at the end of the string.
Parse a text string
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 (or single quotes with usebackq).
Echo just the date from the following string
FOR /F "tokens=4 delims=," %%G IN ("deposit,$4500,123.4,31-AUG-2021") DO @echo Date paid %%G
Parse the output of a command:
FOR /F %%G IN ('"C:\program Files\command.exe"') DO ECHO %%G
Parse the contents of a file:
FOR /F "tokens=1,2* delims=," %%G IN (C:\MyDocu~1\mytex~1.txt) DO ECHO %%G
FOR /F "usebackq tokens=1,2* delims=," %%G IN ("C:\My Documents\my textfile.txt") DO ECHO %%G
Filenameset
To specify an exact set of files to be processed, such as all .MP3 files in a folder including subfolders and sorted by date - just use the DIR /b command to create the list of filenames ~ and use this variant of the FOR command syntax.
Unicode
Many of the newer commands and utilities (e.g. WMIC) output text files in unicode format, these cannot be read by the FOR command which expects ASCII.
To convert the file format use the TYPE command.
Errorlevel
FOR does not, by itself, set or clear an Errorlevel, leaving that to the command being called.
FOR is an internal command.
“It’s completely intuitive; it just takes a few days to learn, but then it’s completely intuitive” ~ Terry Pratchett.
Related commands
FOR - Loop commands.
FOR - Loop through a set of files in one folder, or a list of folders.
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 the output of a command.
FORFILES - Batch process multiple files.
IF - Conditionally perform a command.
SETLOCAL - Control the visibility of environment variables inside a loop.
Equivalent PowerShell: ForEach-Object - Loop for each object in the pipeline.
for example: Get-Content files.txt |Foreach{copy-item -path $_.FullName -destination "H:\destination\"}
Equivalent bash command (Linux): read (in a loop) - Read a line from standard input.
Copyright © 1999-2024 SS64.com
Some rights reserved