Extract all Zip's in a directory (incl. subfolders) with a .bat file or dos command
Solution 1:
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 intoAFolder\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
Solution 2:
This is an update of the accepted answer to support filenames with spaces ("DELIMS=") and skip overwrite (-aos). See links below and updated code. Thanks
Extract all Zip's in a directory (incl. subfolders) with a .bat file or dos command https://stackoverflow.com/questions/12487491/how-to-handle-space-of-filename-in-batch-for-loop http://7zip.bugaco.com/7zip/MANUAL/switches/overwrite.htm
for /F "DELIMS=" %%I IN ('dir /b /s *.zip *.rar') DO (
"H:\Program Files\7-Zip\7z.exe" x -aos -o"%%~dpI" "%%I"
)
Solution 3:
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"
Solution 4:
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