Expand-Archive question...

Welcome Forums General PowerShell Q&A Expand-Archive question...

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

js
 
Participant
3 months ago.

  • Author
    Posts
  • #111592

    Participant
    Points: 0
    Rank: Member

    Hi everyone, new to PowerShell and trying to accomplish something I know if possible but can't quite get it. I need to create a script that can be run as a Scheduled Task that will look for a ZIP file in a specific folder and unzip it to a folder named exactly what the zip file is named. Using Expand-Archive, I'm able to get a zip file with a KNOWN filename unzipped and even a directory created with the same name as the zip file.

    What I can't seem to figure out is, how can I have my script find any zip file, then unzip it to a folder that it creates with the same name as the original zip? The source directory will always be the same.

  • #111596

    Participant
    Points: 261
    Helping Hand
    Rank: Contributor

    You might show the code you already have. It's easier to improve or extend a script than creating something completeley new. 😉

  • #111599

    Participant
    Points: 861
    Helping Hand
    Rank: Major Contributor

    It would be great if you try it yourself first. We are here to help you on any issues you face with the script you write. If you have already started, please share a sample here, we will help you in figuring out the problem.

  • #111602

    Participant
    Points: 289
    Helping Hand
    Rank: Contributor

    Use something like Get-ChildItem to get a list of zip files and iterate over it to figure out the file names. The objects returned by that command have several properties you can access, which you can view in a list by piping to Get-Member and examining the output.

    Using those you can determine the necessary folder names. 🙂

  • #111608
    js

    Participant
    Points: 318
    Helping Hand
    Rank: Contributor
    ls *.zip | foreach { expand-archive $_ }
  • #111616

    Participant
    Points: 318
    Helping Hand
    Rank: Contributor

    As for this...

    ls *.zip | foreach { expand-archive $_ }

    This will simply expand things where they are.

    Take what you are doing to create the folder on the fly, using the zipname returned and then expand in that new folder. This is actually shown in the built-in help files, for you to use directly or tweak as needed.

     

    Get-ChildItem -Path D:\ZIPFiles | Format-Table -AutoSize
    
        Directory: D:\ZIPFiles
    
    
    Mode          LastWriteTime  Length Name    
    ----          -------------  ------ ----    
    -a----   9/4/2018   5:36 PM 2807568 test.zip 
    
    
    Get-ChildItem -Path D:\ZIPFiles -Filter '*.zip' | 
    ForEach{ 
                Expand-Archive -Path D:\ZIPFiles\$($_.Name) -DestinationPath D:\ZIPFiles\$($_.Basename) -Force 
                Get-ChildItem -Path D:\ZIPFiles\$($_.Basename) -Recurse
           }
    • #111628
      js

      Participant
      Points: 318
      Helping Hand
      Rank: Contributor

      Actually, my example makes a subfolder using the basename of the zip file.

The topic ‘Expand-Archive question...’ is closed to new replies.