52

I want to zip a folder containing files. So inorder to do that i need to loop through the entire file list and execute 7za command. (7zip command line version)

for /f %%A in ('"G:\Files Sample\zip\txt\*.t
xt"') do 7za -tzip "%%A.zip" "%%A"

However windows says that this command is not valid.

Error message is

%%A was unexpected at this time

How do i overcome this issue ?

klijo
  • 15,761
  • 8
  • 34
  • 49

3 Answers3

96

%%A is used when you use a batch program (*.bat)

try remove one '%'

Royi Namir
  • 144,742
  • 138
  • 468
  • 792
  • yup that helped, and also there is a point to note, you need to put the dir /b command inside the bracketed region – klijo Feb 16 '12 at 13:37
  • 2
    Also one should note that you can't use something like %file it seems. Only single letter seems to be allowed for parameter. – fschmitt Oct 01 '15 at 06:39
5

If you are doing it from the command line, you don't have to escape the %, so %a is sufficient. You only need to use %%a from batch files.

Also, you wanna be selecting the files instead of executing "G:\Files Sample\zip\txt\*.txt" as a command, which is what the /f switch does in combination with single quotes. The full command would be: for %A in ("G:\Files Sample\zip\txt\*.txt") do 7za -tzip "%A.zip" "%A"

aross
  • 3,325
  • 3
  • 34
  • 42
0

Try this in a batch file.

FOR "G:\Files Sample\zip\txt\" %%G IN (*.txt) DO  7za -tzip "%%G.zip" "%%G"

Add /R as option to search for the files in all subfolder.

A good explanation of cmd- methods you could find at ss64

Andreas Rohde
  • 613
  • 5
  • 15