The command FOR with option /F
is used to process lines of a file, or a single string specified in double quotes, or output of a command line executed by FOR in a separate command process in background captured from handle STDOUT of this background command process.
The first option usebackq
results in interpreting the file name specified in double quotes not as string to process, but as double quoted name of a file of which lines should be processed by FOR.
The option delims=
is used to specify the delimiters for splitting each non empty line not starting with default end of line character ;
up into one or more substrings called tokens. The default delimiters are space and horizontal tab character. delims=
without any character means do not split up the line at all. Only the double quote character "
should be used for this task as delimiter.
The option tokens=
defines which substring(s) should be assigned to the loop variable(s). By default only the first substring (token) is assigned to the specified loop variable. By specifying tokens=2
the command FOR is informed that of interest is the second substring which should be assigned to the specified loop variable (first). This means also if a line does not have a string after 1 or more of the specified delimiters, the line is also ignored. This is true for the first line of input.txt
which does not contain a double quote character at all.
Run in a command prompt window for /?
for more information about the options of command FOR output on several pages.
Those options of FOR are specified usually enclosed in double quotes.
>"output.txt" (
for /F "usebackq tokens=2 delims="" %%A in ("input.txt") do echo "%%A"
)
But the problem here is specifying "
as delimiter. For Windows command interpreter the option string is "usebackq tokens=2 delims="
which would not make much sense because that means the lines should not be split up into tokens and nevertheless only the second token is of interest. However, the next "
is invalid because expected next is the loop variable, a single character with a percent sign before. The percent sign must be escaped in a batch file with an additional percent sign.
For that reason it is necessary to specify the options of FOR without enclosing them in double quotes. So it must be made sure during the preprocessing phase of Windows command interpreter before executing the FOR command line that the 3 options are interpreted as a single argument string.
The escape character to use in batch files is the caret character ^
which instructs Windows command interpreter to interpret the next character as literal character belonging to an argument string. Only %
must be escaped with %
instead of ^
to be interpreted as literal character.
It can be read on running in a command prompt window cmd /?
on last paragraph on last output help page which characters in directory/file names (or other argument strings) require enclosing the string in double quotes: the space character, the characters &()[]{}^=;!'+,`~
and not mentioned by help the characters "|<>
which can't be used in a directory/file name.
So it is specified with usebackq^ tokens^=2^ delims^=^"
for Windows command interpreter that an argument string not enclosed in double quotes with the unusual text usebackq tokens=2 delims="
should be passed to command FOR as second argument after /F
.
Windows command interpreter interprets a space character and an equal sign not escaped with ^
as delimiter between argument strings.
A double quote not escaped with ^
is interpreted as begin of an argument string which ends with another double quote and all characters between with exception of %
and !
when delayed expansion is enabled are interpreted as literal characters.
That is the reason why the spaces characters, the equal signs and the double quote must be escaped with ^
in the argument string usebackq tokens=2 delims="
.
See How does the Windows Command Interpreter (CMD.EXE) parse scripts? for more detailed information about command line parsing by Windows command interpreter.