7zip recursive unzip working well in CMD but not in batch

2

1

I am using this command below to unzip recursively from the source folder(D:\cnosftp\) and put the extracted file in the same subfolder.

FOR /R "D:\cnosftp\" %I IN (*.gz) DO "C:\Program Files\7-Zip\7z.exe" x "%I" -aoa -o"%~dpI"

This is working well when run in CMD - but when same command is put in a batch file and run - it triggers but closes in a blip without any action. Any idea what am I doing here? Or anyone can tell how to wrap it in a batch file.

suyash sikarwar

Posted 2018-09-02T02:58:17.160

Reputation: 21

Use two percent signs in the FOR command for variables. . . FOR /R "D:\cnosftp\" %%I IN (*.gz) DO "C:\Program Files\7-Zip\7z.exe" x "%%I" -aoa -o"%%~dpI" to run it as a batch file. – Pimp Juice IT – 2018-09-02T03:24:17.677

Answers

0

Instead, try running:

FOR /R "D:\cnosftp\" %I IN (*.gz) DO echo "C:\Program Files\7-Zip\7z.exe" x "%I" -aoa -o"%~dpI"

Make sure that the output is what you expect.

Hint: It probably won't be. Replace % with %% as needed. (If two % don't work, try three or four, but I think two might be sufficient.)

The probable issue is that the FOR command may need extra % when run from within a batch file. Using the echo command, to output what command you will actually be running, will often help to determine what variables are expanding the way you want/need them to, and which variables need %s added.

TOOGAM

Posted 2018-09-02T02:58:17.160

Reputation: 12 651