Unzip all zip files in all subfolders

10,208

for /d %%r in (*) do for %%s in ("%%r\*.zip") do echo ---unzip %%s using output folder %%~dps


I suppose zip1 file from your example is zip1.zip and that there could be more zip files in each subfolder. But if not, this will work also for one zip file. And there is an alternative at the bottom of the script for zip files w/o .zip

These files I used for a test. There is an intentious space in "sub folder" because it's always good to test Batch script against pathnames with spaces.

C:\temp\folder\sub folder1\zip1.zip
C:\temp\folder\sub folder1\zip1b.zip
C:\temp\folder\subfolder2\zip2.zip
C:\temp\folder\subfolder2\zip2b.zip
C:\temp\folder\subfolder3\zip3.zip
C:\temp\folder\subfolder3\zip3b.zip
C:\temp\folder\subfolder4\zip4.zip
C:\temp\folder\subfolder4\zip4b.zip

unzip is not a windows command. It can come from many sources and so format of its parameters could differ. In my example, I am using 7zip.
Anyway, you'll probably need to specify output directory with any unzip utility (or CD to it). My sample shows how to obtain its name. There is a slash at the end, which is fine with 7z, but might be a problem with other utility. It's possible to get rid of it, but it would require more code.

You asked to extract all zip files to its own directory. But if two zip files contain files with the same name, they will overwrite each other, that's why it might be more desired to extract each zip to its own folder, with the same name as is the zip file. For this, the folder must be created and the whole script would deserve more than one line.

There is a mistrust against for /d
That's why I also included for /f version which utilize dir command

In case, you would want to use this command more recursively, with deeper paths, like: folder\subfolder3\more\paths\zip1.zip, this is possible. Just use the for /f from my sample below, and add /s to the dir command: 'dir *.* /s /ad /b'.

This command should do what you asked for:
for /d %%r in (*) do for %%s in ("%%r\*.zip") do 7z x -y "%%s" -o"%%~dps"

and here is the whole test-case script:

@echo off

echo ******
echo List all zip files using for /f
echo ******
for /f "tokens=*" %%r in ('dir *.* /ad /b') do for %%s in ("%%r\*.zip") do echo ---unzip %%s using output folder %%~dps

echo.
echo ******
echo List all zip files using for /d
echo ******
for /d %%r in (*) do for %%s in ("%%r\*.zip") do echo ---unzip %%s using output folder %%~dps

echo.
echo 1: extract to folders containing zip files - possible overwrites
echo 2: extract each zip to a folder named by the name of the zip file
choice /c 12
if not errorlevel 2 (

    for /d %%r in (*) do for %%s in ("%%r\*.zip") do 7z x -y "%%s" -o"%%~dps"

) else (

rem ------ this is the alternative to extract each zip to its own folder
    for /d %%r in (*) do for %%s in ("%%r\*.zip") do (
        echo.
        echo ******
        echo *** Unzipping: %%s to folder: %%~dpns
        mkdir "%%~dpns"
        7z x -y "%%s" -o"%%~dpns"
    )
rem ------

)
pause

exit /b

In case zip files does not have .zip extension:

    for /d %%r in (*) do for %%s in ("%%r\*.*") do (
        echo.
        echo ******
        echo *** Unzipping: %%s to folder: %%~dpns-unzipped
        mkdir "%%~dpns-unzipped"
        7z x -y "%%s" -o"%%~dpns-unzipped"
    )
Share:
10,208

Related videos on Youtube

Snickdx
Author by

Snickdx

Computer Science Graduate, acquainted with Java, C, HTML and web development.

Updated on June 04, 2022

Comments

  • Snickdx
    Snickdx 6 months

    Using a cmd batch file how can I iterate all the folders in a directory and unzip all the zip files in each folder? I have been able to get as far as iterating through all the subfolders but I can't iterate within each subfolder.

    FOR /D %%R IN ("*") DO FOR /D %%S IN (*/%RR) unzip %%S
    

    The folder structure

    folder\subfolder1\zip1
    folder\subfolder2\zip2
    folder\subfolder3\zip3
    folder\subfolder4\zip4
    

    I would like to have the contents of each zipfile extracted within it containting folder. The zip files and the subfolders have different names.

    • npocmaka
      npocmaka almost 6 years
      unzip is not standard windows command. Where it comes from?
    • Mofi
      Mofi almost 6 years
      (*/%RR) can't work. A wildcard in a path is not supported by Windows command interpreter. The second percent sign is missing which is obviously a typing mistake as there are two R instead of two %. And the directory separator on Windows is the backslash character and not the slash character. So many mistakes in such a short line. Do you have ever executed in a command prompt window for /? and read the output help for this command?
    • Snickdx
      Snickdx almost 6 years
      nice I didnt know about that help command thanks, I edited the original answer with that command just to improve the question but I pasted what ever I tried last in the batch file in fiddling with the commands.
    • Mofi
      Mofi almost 6 years
      Every internal command of cmd.exe can be executed with parameter /? to get displayed its help. You can also run the command help to get a list of commands. Console applications on Windows can be usually also run with parameter /? to get displayed in the console the brief help for the application. Just console applications ported from Unix need --help or -h to display the help.
    • aschipfl
      aschipfl over 5 years
      By the way: the standard path separator in Windows is \ , not /!
    • papo
      papo over 5 years
      and if you use asterisk in a for /d command's (set), you'll be iterating folders and not files. do is missing at the end, and unzip will probably need to have output folder specified. But your basic idea of how a for loop working is right.
  • Snickdx
    Snickdx almost 6 years
    This was the output, its about 34 subfolders ****** B A T C H R E C U R S I O N exceeds STACK limits ****** Recursion Count=261, Stack Usage=90 percent ****** B A T C H PROCESSING IS A B O R T E D ******
  • npocmaka
    npocmaka almost 6 years
    @Snickdx - the error says that there are 261 nested invokes of CALL command. It's hard to believe it is produced from the code in my answer...
  • Snickdx
    Snickdx almost 6 years
    sorry I seemed to have misinterpreted your answer, I thought the code provided was to be placed in a batch file and named zipjs.bat, but I just realized that its a link to the real script.