How do I extract multiple archives in contained in subdirectories in a folder, outputting the results back into the folders where the archives are.
4 Answers
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
andzip
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.
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.
-
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 08 '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.
- 141
- 3
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
)
-
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 04 '15 at 23:31