Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

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

How do I extract multiple archives in contained in subdirectories in a folder, outputting the results back into the folders where the archives are.

share|improve this question

Firstly, install 7-zip.

Create a bat file in the root of the directory containing many subdirectories with archives inside. Then paste the following in:

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
)

Launch the bat, and all rar's/zips will be extracted into the folder they are contained in.

How does this work?

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

For loop to loop through all folders in the current directory, and put the path into a variable %%F.

pushd %CD%

Put the current directory that we are in into memory.

cd %%F

Set the folder from variable %%F as the current directory.

FOR %%X in (*.rar *.zip) DO (

For all the rar and zip files in the current folder, do:

"C:\Program Files\7-zip\7z.exe" x "%%X"

Run 7-zip on the files. Quotes are needed around %%X because some file names have spaces in them.

popd

Return to the previous directory that we previously stored in the memory.

Hope this is useful to someone.

share|improve this answer

I had problem running the script from Windows Vista. When I ran the code nothing happend. I needed to be administrator to be able to run the script. When I right clicked on the .bat file and "run as administrator" it didn't work because it for some reason started in the system32 folder (if I remember correctly). To solve this simply use the Windows Environment variable (explained here: Windows Environment Variables) %~dp0 to switch back to the directory that the script was run from.

@echo on
cd %~dp0

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
)

Make sure no *.rar or *.zip files are at the same level as the script. They should be one level down.

I hope this comment helped someone.

share|improve this answer
    
This worked fine for me. I had to change the path to "C:\Program Files (x86)" from "C:\Program Files" as I am running 64-bit Win 7. – Contango Aug 8 '13 at 12:02
find . -name "*.zip" |  while read filename; do unzip -o -d "`dirname "$filename"`" "$filename"; done;

Starts a recursive search at the current directory, finds all files ending in .zip, then pipes that into a loop. For every file it finds, it runs an unzip command on the file with the output shunted to the file's directory.

share|improve this answer

The answers above work, however, if you are running Windows 64-bit and 7-Zip 32-bit, the correct path is C:\Program Files (x86)\7-Zip for 7-Zip. Below is the script that worked for me.

@echo on
cd %~dp0

FOR /D /r %%F in ("*") DO ( pushd %CD% cd %%F
    FOR %%X in (*.rar *.zip) DO (
        "C:\Program Files (x86)\7-zip\7z.exe" x %%X
    )
    popd
)
share|improve this answer
    
If you have the 64-bit version of 7zip however, the correct path is: "C:\Program Files\7-zip\7z.exe" as stated in the original answer. – Rohn Adams Jul 4 '15 at 23:31

protected by HopelessN00b Jul 1 at 2:28

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?