Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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
    
I apologize in that case i'll reedit it in that format. Keep in mind that when 7zip recurses a directory, it extracts all the files in one folder rather than the folder where the archive is. –  Dmitri Farkov May 13 '09 at 14:51

5 Answers 5

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

FYI, if you have a Windows 7 64 bit OS, the correct path is "Program Files (x86)" for 7zip. 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

Thanks! To extract also zips in zips you can try this.

@echo on

FOR /D /r %%F in ("*") DO ( 
      pushd %CD% 
     cd %%F 
        FOR %%X in (*.rar *.zip *.tar) DO ( 
                "C:\Program Files\7-zip\7z.exe" x -o"%%~nX" "%%X"
del "%%X"
)
     popd 
)

It should be extended with error checking to avoid deleting files that were not really unzipped. I am so rookie that I am not able to do that..

Could someone tell what does mean ~n in -o"%%~nX" ?

share|improve this answer

Your Answer

 
discard

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