4

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.

2
@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

1

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 :-)

  • Well I count 6 commands plus 7z. And you don't restrict to the given folders. Zip names A,B,C won't need quoting but you never know. gci C:\Archives\*.zip -r|%{&'7z.exe' x $_.FullName -o$_.Directory+$_.BaseName } would do also. – LotPings Aug 24 '17 at 21:05
  • Only the 7z command changes anything on the disk. The others are piped in to the final 7z command. I've edited my answer from "a single PowerShell command" to "a PowerShell one-liner" to reflect your semantic observation. I didn't see anything about "restricting to the given folders" other than the top-level C:\Archives\ in the original question, which is why I didn't attempt that. Either "a ton of subfolders" means "three subfolders", or I was correct not to restrict to the example folders. – FosseWay Aug 25 '17 at 3:27
  • @FosseWay I've tried running the script and i keep getting the following error: Get-ChildItem : A positional parameter cannot be found that accepts argument '%'. At line:1 char:1 + Get-ChildItem -Filter *.zip -Recurse H:\Literature\To-Sort % { $_.Fu ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Get-ChildItem], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand – Chris Jackson Sep 1 '17 at 20:04
  • Sorry to hear that, @ChrisJackson. What happens when you run only Get-ChildItem -Filter *.zip -Recurse C:\Archives on its own? – FosseWay Oct 2 '17 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.