How can I recursively extract zip files on Windows? Each zip file should be unzipped in a new subdirectory, and the zip file should be deleted afterwards.

Each existing or created directory should be scanned for further existing zip files, and so forth.

So the problem is, that I have a huge zip file, which contains many directories with perhaps many further zip files. The original zip files should simply be freed from any zip files, the originial directory tree should be kept, with the convention that each zip file should be represented as a own directory.

So the logic behind is: unzip file in a directory and delete zip file -> go into that directory and unzip all zip files there in the same way -> go into every exisiting subdirectory of the directory and do the same -> and so on recursively

Rough proposal for a recursively programmed batch script file:


  for %%file (%%directory/*.zip) do (unzip %%file | del %%file)

  for /d %%directory (*) do ( call unzip_folder(%%directory) )

  • unzipping a zipped file will automatically recurse into the folders within the zip file. Then you can delete it. Could you edit and add more detail about what you want as it seems likely there is more to it than your description. – Paul May 8 '12 at 10:28
  • It doesn't cover deletion, but check out stackoverflow.com/q/8976287/116891 for at least the recursive unzip scripts. – Pat Sep 13 '13 at 17:04

ExtractNow does this. See http://www.extractnow.com/Usage.aspx#process. Beware that Chrome and maybe other browsers flag the app as malware/spyware. The author maintains that this is just from the installer and points to a portable version for those who don't want the installer (although Chrome also calls that zip file malicious).

  • 1
    ExtractNow wasn't able to extract all of the zip folders, so I ended up writing a batch script that works better (for me anyway). It's in my answer. – Pat Sep 19 '13 at 21:30
  • Tried it just now and it crashed within 5 minutes. I don't even think it was unzipping yet lol. I'll try your script. – Andrew Oct 27 '17 at 2:30

This ought to work for you (works for me). Just be aware that if there are any other folders, it will recurse through them and unzip everything possible. My recommendation: put your zip file in a dir by itself (and this batch file) before running.

:: To actually include the path expansion character (tilde), I had to give valid numbers; see http://ss64.com/nt/rem.html for bug reference. Also, try call /? for more info.
@REM The %~n0 extracts the name sans extension to use as output folder. If you need full paths, use "%~dpn0". The -y forces overwriting by saying yes to everything. Or use -aoa to overwrite.
@REM Using `x` instead of `e` maintains dir structure (usually what we want)

:: If you want recursive, use FOR /R
@FOR /R %%a IN (*.zip) DO @(
    @if [%1] EQU [/y] (
        @7z x "%%a" -o"%%~dpna" -aoa
    ) else if [%1] EQU [/yd] (
        @7z x "%%a" -o"%%~dpna" -aoa
        @if errorlevel 1 (
            @echo There was an error so I won't delete
        ) else (
            REM You can also prompt with del /p
            @del "%%a"
    ) else (
        @echo 7z x "%%a" -o"%%~dpna" -aoa

@echo USAGE: Use /y to actually do the extraction. Use /yd to extract then delete the zip file.
  • Seems legit, tyvm! Note that of course you have to have 7-zip installed and added to your PATH. To do this, I did: setx PATH "%PATH%;C:\Program Files\7-Zip\", restarted Command Prompt, then echo %PATH% to confirm it was added in, and finally unzip.bat (what I named it), where unzip.bat is within the root directory you want to unzip from. – Andrew Oct 27 '17 at 2:39
echo %rant%

Well, here we go... the result of hours of research, failed attempts, and much fiddling (even tried PowerShell - doesn't support long file paths out of the box - sheesh!)... a version that actually recursively extracts archives and puts them all in a single folder for deletion...

I took Pat's answer and did a lot of revising... to support... long file paths beyond 260 characters in length!:

@echo off
setlocal enabledelayedexpansion enableextensions

set scriptDir=%~dp0

REM Clear the log files.
echo . > unzipLog.txt
echo . > unzipErrors.txt

mkdir DeleteMe >> unzipLog.txt 2>nul

REM Recurse through all common compressed archive files.
FOR /R %%a IN (*.zip,*.7z,*.rar,*.tar,*.gz) DO (
    @echo: >> unzipLog.txt 2>> unzipErrors.txt
    @echo: >> unzipLog.txt 2>> unzipErrors.txt
    @echo: >> unzipLog.txt 2>> unzipErrors.txt
    @echo: >> unzipLog.txt 2>> unzipErrors.txt
    @echo: >> unzipLog.txt 2>> unzipErrors.txt

    REM Prepend \\?\ to the beginning of each path to handle paths longer than 260 characters.
    if [%1] EQU [/y] (
        REM Extract only.
        7z x "\\?\%%a" -o"%%~dpna" -aoa >> unzipLog.txt 2>> unzipErrors.txt
    ) else if [%1] EQU [/yd] (
        REM Extract and delete.

        for %%b in ("%%a") do (
            set p=%%~dpb
            set f=%%~nxb

        IF !p:~-1!==\ SET p=!p:~0,-1!

        echo "!p!" "!scriptDir!DeleteMe" "!f!"
        echo "!p!" "!scriptDir!DeleteMe" "!f!" >> unzipLog.txt 2>> unzipErrors.txt

        7z x "\\?\%%a" -o"%%~dpna" -aoa >> unzipLog.txt 2>> unzipErrors.txt

        if errorlevel 1 (
            echo There was an error so I won't delete >> unzipLog.txt 2>> unzipErrors.txt
        ) else (
            robocopy "!p!" "!scriptDir!DeleteMe" "!f!" /MOVE /FP /NS /NC /NFL /NDL /NP /IS /IT /SL >> unzipLog.txt 2>> unzipErrors.txt
    ) else ( 
        REM Just echo.
        echo 7z x "\\?\%%a" -o"%%~dpna" -aoa >> unzipLog.txt 2>> unzipErrors.txt

REM Can comment this out if you just want to extract the archives to a folder and not delete them...:
REM WARNING: recommended call this manually and very carefully!!!
REM rmdir /S /Q DeleteMe
REM WARNING: recommended call this manually and very carefully!!!

echo Use /y to actually do the extraction. Use /yd to extract then delete the zip file.
echo See unzipLog.txt and unzipErrors.txt!

  • 1
    Seems to have issues with ! in directory names, and it pauses and silently prompts whenever it needs a password... (Should still work fine though overall, and I can't imagine it failing in any destructive way.) If I ever get around to updating this I might revise my answer, although, if I do, I'll probably just use the newer PowerShell that supports long paths...: stackoverflow.com/a/47046785/1599699 – Andrew Nov 1 '17 at 6:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.