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 into AFolder\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
Great answer! One of the best I've ever seen. Thoroughly, yet clearly and concisely, explained. Awesome job! – ksoo – 2015-11-30T13:50:30.737
Would it be hard to expand the script so I can add a hard coded pathway for all the folders to be extracted too? So rather than extracting in current location, extract to a new folder pathway? – Austin – 2016-10-10T14:36:41.583
@Austin I don't have access to it right now, but 7-Zip comes with an excellent help file. There's a section for everything 7-Zip can do from the command line, and I bet what you want is there. – Stephen Jennings – 2016-10-10T15:37:58.500
2Note that this won't work if the directories or filenames have space or tab in them. To work with those, you need to specify the option "delims=" to remove those as token delimiters. – Thought – 2017-05-18T01:25:18.263