locked
how to unzip the latest file in a folder RRS feed

  • General discussion

  • I have found this code on internet which is perfect the only problem is how to use get-childitem in this code so that it copies the most recent zip file and unzips it. here is the code.

    $path = "C:\Deployments"
    $destination = "C:\_Temp"
    $shell_app= New-Object -com shell.application

    $files = Get-ChildItem -Path $path -filter *.zip -recurse

       foreach($file in $files) {

        $zip_file = $shell_app.namespace($file.FullName)

         $copyHere = $shell_app.namespace($destination)

      $copyHere.Copyhere($zip_file.items())

    }

       
    • Edited by k786 Wednesday, September 24, 2014 3:10 PM
    Wednesday, September 24, 2014 2:53 PM

All replies

  • It is more than likely that this will fail as the sheel zip functions are not designed to work with script.

    To get the latest file in any folder set we would do this.

    $file=Get-ChildItem c:\temp -File | Sort LastWriteTime -desc | Select -first 1


    ¯\_(ツ)_/¯

    Wednesday, September 24, 2014 3:29 PM
  • Thanks this is sorted
    Wednesday, September 24, 2014 3:42 PM
  • We have already explained that you should not use the GUI shell methods for automation. They are not designed for this.

    http://social.technet.microsoft.com/Forums/scriptcenter/en-US/8ddb1843-929f-4028-80d1-f8ac42891f54/

    Please read this thread carefully.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, September 24, 2014 3:49 PM
  • If you have PowerShell 3 or later you can do this:

    PS C:\scripts> Add-Type  -AssemblyName System.IO.Compression.FileSystem
    PS C:\scripts> [System.IO.Compression.ZipFIle]::ExtractToDirectory('c:\scripts\scrips.zip','c:\temp2\scrips')
    PS C:\scripts> dir c:\temp2\scrips
    
    
        Directory: C:\temp2\scrips
    
    
    Mode                LastWriteTime     Length Name
    ----                -------------     ------ ----
    -a---         8/28/2013   5:36 PM        306 aclfile.txt
    -a---         8/13/2013   1:59 PM       2270 Add-FolderAce.ps1
    -a---         8/27/2013   3:50 PM       1532 BackupSecurityLog.vbs
    -a---          7/1/2013  12:52 PM       8154 Book1.xlsx
    -a---         8/30/2013   1:12 PM        330 cdcatalog.xml
    -a---         8/30/2013   2:38 PM        610 cdcatalog.xsl
    -a---         7/29/2013   5:38 PM         58 config.ini
    -a---         7/25/2013  11:33 AM       2596 configfile.xml
    -a---         7/20/2013  12:20 PM        751 consumer.vb
    -a---         7/20/2013  11:43 AM        352 consumer3.vbs
    -a---         8/29/2013  10:56 AM       3122 CreateXMlDoc.vbs
    -a---         8/29/2013   1:12 PM         62 csv1.csv
    -a---         8/29/2013   1:12 PM         32 csv2.csv
    -a---         6/25/2013   5:39 PM      60213 CSV_Parser.ps1
    -a---         8/19/2013   1:53 PM     450560 Database1.accdb
    -a---         8/11/2013   6:46 AM      17580 dump.txt
    -a---         7/23/2013   6:19 PM      28250 energy-report.html
    -a---         7/20/2013  11:41 AM        666 fileevnts.mof
    -a---         7/20/2013  12:57 PM        873 fileevnts2.mof
    -a---          8/7/2013  12:52 PM          7 foo.ps1
    -a---          8/7/2013   1:14 PM        166 foo2.ps1
    -a---         8/27/2013   1:55 PM       1850 Get-Drives.ps1
    -a---         8/28/2013  12:51 PM       1432 Get-LogData.ps1
    -a---         6/25/2013   4:54 PM        995 Get-RemoteSession.ps1
    -a---         8/21/2013   4:34 PM      10942 Get-RemoteUpdates.ps1


    ¯\_(ツ)_/¯

    Wednesday, September 24, 2014 4:03 PM