11

I think I'm missing something because I can't seem to find what this means.

Example:

for /D %%A in (*) do "\7za.exe" u -t7z -m9=LZMA2 "%%A.7z" "%%A"

That line was supposed to use a command line version of 7zip to compress individual folders, but I'm stumped as to what %%A means in this context.

Azat Ibrakov
  • 103
  • 5
Jim Kieger
  • 265
  • 1
  • 3
  • 9
  • 2
    [What is the difference between % and %% in a cmd file?](http://stackoverflow.com/questions/14509652/what-is-the-difference-between-and-in-a-cmd-file) – slhck Nov 07 '13 at 17:26
  • 1
    Got command line and bath file confused. Changed the sign on top. – Jim Kieger Nov 08 '13 at 02:32

3 Answers3

15

The for command needs a placeholder so you can pass along variables for use later in the query, we are telling it use the placeholder %A, the reason the code you saw uses %%A is because inside a batch file (which I assume is where you found this) the % has a special meaning, so you must do it twice %% so it gets turned in to a single % to be passed to the for command

To actually break apart what the command is doing, there is two parts to the command:

 for /D %%A in (*) do .....

What this part says is for every folder in the current folder execute the following command replacing %%A with the name of the currently processing folder.

..... "\7za.exe" u -t7z -m9=LZMA2 "%%A.7z" "%%A"

What this part says is execute the command "\7za.exe" u -t7z -m9=LZMA2 "%%A.7z" "%%A" and replace the two %%A's with the current record we are processing.

DavidPostill
  • 153,128
  • 77
  • 353
  • 394
Scott Chamberlain
  • 30,694
  • 7
  • 96
  • 109
  • Just to nitpick - the /D on the for loop will limit the * wildcard to directories, not "every file in this folder and every subfolder" (assuming Command Extensions are enabled). – ernie Nov 07 '13 at 17:35
  • @ernie you are correct, I was looking at the /R switch on the help page, I have corrected my answer. – Scott Chamberlain Nov 07 '13 at 17:36
  • Thank you for that, it shows how noobish this seems, but it's pretty archaic stuff that I have to dig through a couple online manuals for. – Jim Kieger Nov 07 '13 at 17:48
0

It's a variable.

That particular example uses the directory option of a FOR loop, iterating through the directories and assigning them to %%A.

That's also not a command-line example, but a batch file example. In batch files, you need to use %%A, while on the command-line, you'd just use %A.

ernie
  • 6,293
  • 2
  • 28
  • 30
0

In your scenario, the %%A is a placeholder for what the "for" loop is iterating over (which the /D indicates directories). So each iteration of the loop, %%A is one of the directories.

You'll see %% instead of % in batch code. You'll see % instead of %% used in your command prompt.

So know that if you copy over a batch file code into a command prompt and run it with %% being used, it will error, and vice versa.

Anthony Miller
  • 1,594
  • 4
  • 16
  • 23