0
votes

I have multiple zip files in different folders and want to extract them into the folder they are in.

Parent Folder
    -Folderwithzip1
        --zip1
        --[Content from zip1 should land here]
    -Folderwithzip2
        --zip2
        --[Content from zip2 should land here]

I've tried

for /r "C:\zip" %f in (*.zip) do do 7z x -o "%~dpf" "%f"

I also tried

FOR /F "USEBACKQ tokens=*" %%F IN (`DIR /b *.zip`) DO ("C:\Program Files\7-Zip\7z.exe" e "%%F" -y)

but that does only extract the files in the folder where the batch file sits.

But the batch does not even start.

1

1 Answers

0
votes

The command line contains twice the keyword do which is the first mistake.

The second mistake is the space character between -o and "%~dpf".

The help of 7-Zip is the file 7zip.chm in program files folder of 7-Zip which can be either opened from within 7-Zip or by double clicking on this file. There is on tab Contents the list item Command Line Version with the sublist item Switches containing the link to -o (set Output directory) switch. The syntax explained on this help page as well as the examples don't have a space between switch -o and directory path.

7-Zip also outputs an appropriate error message on execution with a space between -o and the output directory path:

Command Line Error:
Too short switch:
-o

So the solution is quite simple for the command line usage:

for /R "C:\zip" %I in (*.zip) do "C:\Program Files\7-Zip\7z.exe" x -o"%~dpI" -y -- "%I"

And for usage in a batch file with each percent sign escaped with one more percent sign:

for /R "C:\zip" %%I in (*.zip) do "C:\Program Files\7-Zip\7z.exe" x -o"%%~dpI" -y -- "%%I"

I suggest also running in a command prompt window for /? and read the help output for command FOR.

DIR is not needed here except there are also *.zip files with hidden attribute set which are always ignored by FOR or some of the ZIP files contain itself *.zip files and extracting those *.zip files should be avoided on running the batch file. Run in a command prompt window dir /? for help on command DIR.

The following command line can be used in a batch file to first run the command DIR in a separate command process started by FOR with using cmd /C, capture everything written by DIR to handle STDOUT and then process all captured lines (= full qualified file names) line by line:

for /F "delims=" %%I in ('dir "C:\zip\*.zip" /A-D /B /S 2^>nul') do "C:\Program Files\7-Zip\7z.exe" x -o"%%~dpI" -y -- "%%I"

DIR option /S is required here to let DIR search also in all subdirectories of C:\zip for *.zip files and get output the name of the found *.zip files with full path instead of file name only.