Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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)?

share|improve this question

migrated from serverfault.com Dec 25 '11 at 2:39

This question came from our site for system and network administrators.

up vote 3 down vote accepted

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
share|improve this answer
    
Great answer! One of the best I've ever seen. Thoroughly, yet clearly and concisely, explained. Awesome job! – ksoo Nov 30 '15 at 13:50

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"

share|improve this answer
    
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 '11 at 2:01
1  
Have you considered using powershell instead? Should be a long one-liner. – Jim B Dec 25 '11 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%? – Miles Erickson Dec 26 '11 at 2:00
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.

share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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