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

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
1 week, 4 days ago.

  • 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: 274
    Points: 153
    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.

You must be logged in to reply to this topic.