Batch unzip all zip archives in sub directories; content of each archive extracted to new directory named after the archive
I have a folder with a ton of sub folders, within those subfolders are zipped files.
I was wondering how to unzip those files into a folder named after the zip file So for example:
C:\Archives\Photos\A.zip
C:\Archives\Videos\B.zip
C:\Archives\Documents\C.zip
would turn into
C:\Archives\Photos\A\
C:\Archives\Videos\B\
C:\Archives\Documents\C\
I can do this currently but only if i do it one folder at at time, i was wondering if there was a way to do it all at once, I was thinking a .bat file.
@Echo off
For %%F in (Photos,Videos,Documents) Do (
For /F "delims=" %%Z in ('Dir /B/S/A-D "C:\Archives\%%F\*.zip" 2^>Nul') Do (
7z x "%%~fZ" -o"%%~dpnZ\" && Rem Del "%%~fZ"
)
)
If you want to discard the zip file once successfully unzipped, remove the Rem
in front of del.
The batch uses for variable ~ modifiers to use only parts of the full file name
A PowerShell one-liner to achieve this:
Get-ChildItem -Filter *.zip -Recurse C:\Archives | % { $_.FullName } | Split-Path | Get-Unique | % { cd $_ ; &'C:\Program Files\7-Zip\7z.exe' x *.zip -o* }
Here's an explanation of each section, which is piped into the next section and thus executed by Powershell in order:
- Get-ChildItem -Filter *.zip -Recurse C:\Archives: find all zip files under C:\Archives
- % { $_.FullName }: foreach resulting object, show the full path & name
- Split-Path: show only the path (not the filename) of each zip
- Get-Unique: remove duplicates from that list
- % { cd $_ ; &'C:\Program Files\7-Zip\7z.exe' x *.zip -o* }: finally, use the created list to change into each directory found; use 7z.exe to unzip all zip files found in each directory, with the -o* argument to unzip the contents of each zip file into a directory with the name of that zip file
Assumptions:
- C:\Archives is the directory under which all your zip files are stored
- Your 7-Zip is installed in the default location
While researching this question, I came to the conclusion that I prefer bash to PowerShell :-)