Unzip multiple zips to multiple folders from a single source folder

Welcome Forums General PowerShell Q&A Unzip multiple zips to multiple folders from a single source folder

Viewing 2 reply threads
  • Author
    Posts
    • #181770
      Participant
      Topics: 2
      Replies: 2
      Points: 15
      Rank: Member

      Hi,

      I'm trying to write a script to unzip multiple zip files to multiple folders. Essentially I want to be able type in the source and destination folders that contain 10s or 100s of zip files and have it create the folders and start unzipping each zip file to the folder with the same name.

      I originally tried doing this with loops and the Expand-Archive cmdlet but I ran in to some problems and now I'm trying with the .Net framework class for file compression.

      I've been able to put together the below code which kind of works but it added all the folder names in the destination path together to make the new folder to move the extracted files. Right now I'm kind of stuck for ideas about how I can fix this and extract to all separate folders, like in the example of bash code below the powershell script.

      Your help with this is much appreciated!

      $src = "C:\Temp\zip\"
      $dest = "C:\Temp\unzip\"
      
      Add-Type -AssemblyName System.IO.Compression.FileSystem
      $zps = Get-ChildItem $src -Filter *.zip
      $foldernames = Get-ChildItem $dest
      $newfolders = $dest + $foldernames
      
      
      foreach ($zp IN $zps)
      {
      $all = $src + $zp
      [System.IO.Compression.ZipFile]::ExtractToDirectory($all, $newfolders)
      }

       

      A Linux world example of what I want to achieve:

      find -name '.zip' -exec sh -c 'unzip -d "${1%.}" "$1"' _ {} \;
    • #181791
      Participant
      Topics: 5
      Replies: 285
      Points: 213
      Helping Hand
      Rank: Participant

      This example will work with PowerShell version 5.

      # Unzip each zip file into its own directory inside destination
      $zip = Get-ChildItem -Path C:\Temp\zip\ -Filter *.zip
      foreach ($z in $zip){
          Expand-Archive -Path $z.FullName -DestinationPath C:\Temp\unzip\$($z.BaseName) -Verbose
      }
      
    • #181839
      Participant
      Topics: 2
      Replies: 2
      Points: 15
      Rank: Member

      Perfect, using the basename dot operator solves the problem. Thanks for the help.

Viewing 2 reply threads
  • The topic ‘Unzip multiple zips to multiple folders from a single source folder’ is closed to new replies.