Arbitrary. You can use any letter, upper or lower, and even symbols.
for %%# in... do command %%#
would work just as well. But when working with multiple tokens per iteration, it's better to use the alphabet. Here's an example why:
for /f "usebackq tokens=1* delims==" %%I in ("textfile.txt") do (
set "config[%%~I]=%%~J"
)
This is because %%I
contains the text matched prior to the first equal sign, and %%J
contains everything after the first equal sign. This answer shows that example in context.
The answer to your question is hinted in the for
command's documentation. help for
in the cmd console for full details. Specifically:
Some examples might help:
FOR /F "eol=; tokens=2,3* delims=, " %i in (myfile.txt) do @echo %i %j %k
would parse each line in myfile.txt, ignoring lines that begin with
a semicolon, passing the 2nd and 3rd token from each line to the for
body, with tokens delimited by commas and/or spaces. Notice the for
body statements reference %i to get the 2nd token, %j to get the
3rd token, and %k to get all remaining tokens after the 3rd.
This page explains further:
FOR Parameters
The first parameter has to be defined using a single character, for example the letter G.
FOR %%G IN ...
In each iteration of a FOR loop, the IN ( ....) clause is evaluated and %%G
set to a different value
If this clause results in a single value then %%G
is set equal to that value and the command is performed.
If the clause results in a multiple values then extra parameters are implicitly defined to hold each. These are automatically assigned in alphabetical order %%H %%I %%J
...(implicit parameter definition)
If the parameter refers to a file, then enhanced variable reference can be used to extract the filename/path/date/size.
You can of course pick any letter of the alphabet other than %%G
.
%%G
is a good choice because it does not conflict with any of the pathname format letters (a, d, f, n, p, s, t, x) and provides the longest run of non-conflicting letters for use as implicit parameters.
G > H > I > J > K > L > M
Format letters are case sensitive, so using a capital letter is also a good way to avoid conflicts %%A
rather than %%a
.
Just in the interest of thoroughness, it should be pointed out that:
- when using
for
in a cmd console, use single percents.
- when using
for
in a bat script, use double percents.
- Use a tilde when retrieving the iterative variable to strip surrounding quotation marks from the value. (e.g.
"Hello world!"
becomes Hello world!
). It's convenient to use this to force a desired format. "%%~G"
would always be quoted, whether the captured value was quoted or not. You should always do this when capturing file names with a for
loop.
- Tilde notation also allows expanding paths, retrieving file size, and other conversions. See the last couple of pages of
help for
in a cmd console for more details.