Windows – How to extract all archives in the subdirectories of this folder

batchwindows

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

Best Answer

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.