6

I want to extract all .zip's and .rars in a folder and its children

The structure is like this:

MAIN_FOLDER
    -A folder
        - a.zip
            -a.rar
    -B folder
        - b.zip
            -b.rar
    -C folder
        ....    
            ...

I tried already this, which did not work

FOR /D /r %%F in ("*") DO (

  pushd %CD%
 cd %%F
    FOR %%X in (*.rar *.zip) DO (
        "C:\Program Files\7-zip\7z.exe" x %%X
    )
 popd

)

I use Windows and have 7-Zip installed.

Additional Question: Would it be possible to save all the extracted files from the last children (a.rar, b.rar) in one and the same folder (main folder)?

6 Answers 6

13

The Script:

for /F %%I IN ('dir /b /s *.zip *.rar') DO (
    "C:\Program Files\7-Zip\7z.exe" x -o"%%~dpI" "%%I"
)

Explanation:

for /F %%I IN ('dir /b /s *.zip *.rar') DO (

This performs a loop for each file returned by the command dir /b /s *.zip *.rar. The /s tells dir to recurse into subdirectories and /b prints in bare format.

The filename is stored in the %%I variable for use later. If you were typing this at the prompt, you would use %I instead.

"C:\Program Files\7-Zip\7z.exe" x -o"%%~dpI" "%%I"

This performs the extraction. The argument -o"%%~dpI" extracts the file into the same directory where the archive resides. Other options:

  • -o"%%~dpI" — Extracts into the directory where the archive resides.

  • -o"%%~dpnI" — Creates a new directory in the hierarchy named after the archive and extracts there (that is, AFolder\archive.zip extracts into AFolder\archive\).

  • -o"%%~nI" — Creates a new directory in the current directory named after the archive and extracts there (that is, AFolder\archive.zip extracts into .\archive\).

  • Omit the -o argument — Extracts into the current directory.

Example:

C:\Temp>tree /F

    Folder PATH listing
    Volume serial number is 08A4-22E0
    C:.
    │   batch.bat
    │
    ├───AFolder
    │       a.zip
    │
    ├───BFolder
    │       b.zip
    │
    └───CFolder
            c.zip



C:\Temp>batch.bat > nul


C:\Temp>tree /F

    Folder PATH listing
    Volume serial number is 08A4-22E0
    C:.
    │   batch.bat
    │
    ├───AFolder
    │       a.zip
    │       a.zip.txt
    │
    ├───BFolder
    │       b.zip
    │       b.zip.txt
    │
    └───CFolder
            c.zip
            c.zip.txt
5
  • Great answer! One of the best I've ever seen. Thoroughly, yet clearly and concisely, explained. Awesome job!
    – Stan
    Nov 30, 2015 at 13:50
  • Would it be hard to expand the script so I can add a hard coded pathway for all the folders to be extracted too? So rather than extracting in current location, extract to a new folder pathway?
    – Austin
    Oct 10, 2016 at 14:36
  • @Austin I don't have access to it right now, but 7-Zip comes with an excellent help file. There's a section for everything 7-Zip can do from the command line, and I bet what you want is there. Oct 10, 2016 at 15:37
  • 3
    Note that this won't work if the directories or filenames have space or tab in them. To work with those, you need to specify the option "delims=" to remove those as token delimiters.
    – Thought
    May 18, 2017 at 1:25
  • Thanks a lot for sharing this. Could you please also share further code changes to modify script to process/extract a directory & its sub-directories without any pop-up (or user input) by automatically selecting "No" if in case file to be extracted is already present. Currently we keep getting these pop-ups to provide user inputs if in case file to be extracted is already present in a directory/sub-directory. Jul 7, 2022 at 13:06
3

This is an update of the accepted answer to support filenames with spaces ("DELIMS=") and skip overwrite (-aos). See links below and updated code. Thanks

Extract all Zip's in a directory (incl. subfolders) with a .bat file or dos command https://stackoverflow.com/questions/12487491/how-to-handle-space-of-filename-in-batch-for-loop http://7zip.bugaco.com/7zip/MANUAL/switches/overwrite.htm

for /F "DELIMS=" %%I IN ('dir /b /s *.zip *.rar') DO (
    "H:\Program Files\7-Zip\7z.exe" x -aos -o"%%~dpI" "%%I"
)
1
  • for /F "DELIMS=" %%I IN ('dir /b /s *.zip *.rar') DO ( "C:\Program Files\7-Zip\7z.exe" x -aos -o"%%~dpI" "%%I" )
    – acruns
    Oct 22, 2017 at 15:33
2

I believe that you are looking for the forfiles command:

  • forfiles /s /m *.zip /c "7z x @file"

  • forfiles /s /m *.rar /c "7z x @file"

3
  • thanks for your tip. it does not work like intented. sorry i am not that firm in dos commands. would someone mind writing working code? thx and anyway merry christmas :)
    – Email
    Dec 25, 2011 at 2:01
  • 1
    Have you considered using powershell instead? Should be a long one-liner.
    – Jim B
    Dec 25, 2011 at 6:27
  • 2
    @Email (1) What error are you receiving? Saying "it does not work" without detail is not constructive, and "would someone mind writing working code?" is insulting. (2) Is 7z.exe's program directory in %PATH%? Dec 26, 2011 at 2:00
1

I just use sweep.exe from years ago. It runs the same command in the current directory and all subdirectories.

You may have to run sweep more than once if you are looking to extract archives within an archive.

You can use something like:

sweep 7za x -y *.zip

to open all .zip files in the current folder and all folders underneath.

7zip command line version is here: http://www.7-zip.org/download.html

0
7z e -an -air!*.rar -r

extracts all rar files from the current directory's sub folders (recursively) into the current folder. Change to -air!*.zip for zip files.

0
@echo off

FOR /R %%g IN (.) DO TITLE Extracting from %%~ng & "%~dp07z.exe" x -aou "%%g\*.rar" -o"%%g" & "%~dp07z.exe" & CLS

Copy that to a text file and rename it to extract.bat and put it in the parent folder containing the other folders with archives. Put the 7z.exe in the same folder as the extract.bat file, or modify the path in the command above to reflect the installed location of your 7z.exe. This extracts all RARs in each subfolder to the same subfolder as the RAR. It does not do any "house cleaning" (like my girlfriend)...

Adjust the *.rar to fit your needs.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .