2

I am looking for a list of attributes and what they do in a for loop inside the command prompt.

Specifically I have a .bat file that copies a file from the root of the C:\ drive and pastes it inside all folders found in a pre-specified directory (i.e. C:\Users\John\Test Directory).

This is the command:

@echo off
for /D %%a in (C:\Users\John\Test Directory\*.*) do xcopy /y /d C:\test_file.txt "%%a\"

The .bat does exactly what I need it to do, but I do not understand what the "%%a" does in the command. I see similar commands that use %%g, %%f, etc, but nothing that defines why those were chosen or what they specifically do. Are those attributes arbitrary or do they have a defined function? I seemingly can't find any information on the attributes so any insight is appreciated!

Ross Ridge
  • 38,414
  • 7
  • 81
  • 112
Trevor
  • 141
  • 11
  • Type `for /?` into a command prompt window and read the help text; you will see that you need to specify a variable `%variable`, which consists of a single letter (in a case-sensitive manner), and that you need to double the `%` sign in case you are using the `for` command line in a batch program... – aschipfl May 09 '16 at 19:55
  • Yes, I have read that, but it does not specify if that letter is arbitrary or if each letter has a specified function, that is my question. – Trevor May 09 '16 at 20:08

1 Answers1

5

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.
Community
  • 1
  • 1
rojo
  • 24,000
  • 5
  • 55
  • 101