Expand-Archive to Screen (Console)

This topic contains 9 replies, has 3 voices, and was last updated by Profile photo of Curtis Smith Curtis Smith 4 months ago.

  • Author
    Posts
  • #66181
    Profile photo of Malcolm J
    Malcolm J
    Participant

    Hi all, bit stuck with 140 zip files (created by windows 7 backup).
    I have to search all of those for a particular file type &
    I don't want to extract them to HDD – just see on screen what's in what.

    Hope that's clear – any help appreciated.

    Cheers Johnboy

  • #66183
    Profile photo of Olaf Soyk
    Olaf Soyk
    Participant

    Do you wnat to do this on a regular base? Does it have to be a script? If not you could try this:

    http://www.techkranti.com/2010/03/search-within-zip-files-in-windows-7.html

    • #66216
      Profile photo of Malcolm J
      Malcolm J
      Participant

      Hi Olaf, many thanks for your interest.
      This would be a bit cumbersome with 215 .zip files
      and would not be much different from opening with winzip.

      I am hoping to create a function to do this when I know
      a bit more about Powershell – at the current rate of progress
      that will be about 2030!!

      Thanks again

      Regards Malcolm J.

    • #66225
      Profile photo of Olaf Soyk
      Olaf Soyk
      Participant

      This would be a bit cumbersome with 215 .zip files
      and would not be much different from opening with winzip.

      That's the point – when I understood it right you could use the normal windows explorer search to find what you're looking for. You wouldn't have to open all single files for that.

  • #66195
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    You could do something like this.

    $ext = "jpg"
    $parentPath = "D:\Support"
    Add-Type -Assembly "system.io.compression.filesystem"
    Get-ChildItem "$parentPath\*.zip" -Recurse -File | ForEach-Object {
        $files = [io.compression.zipfile]::OpenRead($_.FullName) | Select-Object -ExpandProperty Entries | Where-Object {$_.Name -like "*.$ext"}
        if ($files) {
            "Zip file: $($_.FullName)"
            $files | ForEach-Object {"`t$($_.Name)"}
        }
    }
    • #66213
      Profile photo of Malcolm J
      Malcolm J
      Participant

      Hi Curtis, Many thanks for your interest with this problem.
      I'm afraid this is way above my understanding of the language,
      I've been "playing with" the Read-Archive cmdlet – without much success!
      Thanks again.
      Regards Malcolm J.

    • #66228
      Profile photo of Olaf Soyk
      Olaf Soyk
      Participant

      I think it is much less hard than you probably think. If you want to start anyway – here are some good starting points:

      https://powershell.org/forums/topic/using-csv-files/

  • #66264
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    I am not familiar with read-archive as it is not a native cmdlet. I have made notes on the previously provided coded to indicate what is happening at each step. I hope this helps with the understanding.

    #Set variable the extension of the file type you wish to find
    $ext = "jpg"
    
    #Set variable for the parent directory that contain .zip files
    $parentPath = "D:\Support"
    
    #Add the system.io.compression.filesystem .Net assembly which contains methods for reading .zip files
    Add-Type -Assembly "system.io.compression.filesystem"
    
    #Use Get-ChildItem to return all .zip files in the Parent directory, recursively
    Get-ChildItem "$parentPath\*.zip" -Recurse -File |
    
    #Use ForEach-Object to perform the additional actions for each .zip file
    ForEach-Object {
        #Use the OpenRead method of [io.compression.zipfile], which is made available by the Add-Type command above, to open the current .zip file in readmode.  This returns a object with multiple propeties including an Entries property that contains the list of files in the .zip
        $files = [io.compression.zipfile]::OpenRead($_.FullName) | 
    
        #Use Select-Object and -ExpandProperty to get just the file list
        Select-Object -ExpandProperty Entries | 
    
        #Filter the list so that only the files with the extension stored in $ext are returned, and store results in $files
        Where-Object {$_.Name -like "*.$ext"}
    
        #If files were found
        if ($files) {
            
            #Output the name of the zip file currently being processed
            "Zip file: $($_.FullName)"
    
            #Output each found file name, preceeded by a tab character
            $files | ForEach-Object {"`t$($_.Name)"}
        }
    }

    Additionally, it would really be better to build new custom objects and output those to the pipeline for further processing instead of what I'm doing here by just outputing the string results to show in the console. But, I will let you add that if you desire.

    • #66279
      Profile photo of Malcolm J
      Malcolm J
      Participant

      Curtis Many thanks for your time once again.
      I copied your code and made the necessary modification to the $parentPath.
      The results are shown below.
      PS C:\WINDOWS\system32>
      $ext = "jpg"
      $parentPath = "L:\JOHN-PC\Backup Set 2017-02-28 160001\Backup Files 2017-03-09 142241"
      Add-Type -Assembly "system.io.compression.filesystem"
      Get-ChildItem "$parentPath\*.zip" -Recurse -File | ForEach-Object {
      $files = [io.compression.zipfile]::OpenRead($_.FullName) | Select-Object -ExpandProperty Entries | Where-Object {$_.Name -like "*.$ext"}
      if ($files) {
      "Zip file: $($_.FullName)"
      $files | ForEach-Object {"`t$($_.Name)"}
      }
      }

      Exception calling "OpenRead" with "1" argument(s): "End of Central Directory record could not be found."
      At line:6 char:5
      + $files = [io.compression.zipfile]::OpenRead($_.FullName) | Select ...
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
      + FullyQualifiedErrorId : InvalidDataException

      Zip file: L:\JOHN-PC\Backup Set 2017-02-28 160001\Backup Files 2017-03-09 142241\Backup files 6.zip
      `tBlueToothKeyboard (2).jpg
      `tBlueToothKeyboard.jpg
      `tBlueToothKeyboard.jpg
      `tDSCF0001.JPG
      `tDSCF0003.JPG
      `tDSCF0004.JPG
      `tDSCF0005.JPG
      `tDSCF0006.JPG
      `tDSCF0007.JPG
      `tDSCF0008.JPG
      `tDSCF0012.JPG
      `tDSCF0013.JPG
      `tDSCF0014.JPG
      `tDSCF0015.JPG
      `tDSCF0020.JPG
      `tDSCF0021.JPG
      `tDSCF0024.JPG
      `tDSCF0025.JPG
      `tDSCF0026.JPG
      `tDSCF0027.JPG
      `tDSCF0028.JPG
      `tDSCF0029.JPG
      `tDSCF0030.JPG
      `tDSCF0031.JPG
      `tDSCF0032.JPG
      `tDSCF0033.JPG
      `tDSCF0034.JPG
      `tDSCF0035.JPG
      `tDSCF0037.JPG
      `tDSCF0040.JPG
      `tDSCF0049.JPG
      `tDSCF0050.JPG
      `tDSCF0051.JPG
      `tDSCF0052.JPG
      `tDSCF0053.JPG
      `tDSCF0054.JPG
      `tDSCF0055.JPG
      `tDSCF0056.JPG
      `tDSCF0060.JPG
      `tDSCF0061.JPG
      `tDSCF0084.JPG
      `tDSCF0088.JPG
      `tDSCF0092.JPG
      `tDSCF0093.JPG
      `tDSCF0097.JPG
      `tDSCF0098.JPG
      `tDSCF0099.JPG
      `tDSCF0066.JPG
      `tDSCF0067.JPG
      `tDSCF0068.JPG
      `tDSCF0069.JPG
      `t_SCF0010.JPG
      `t_SCF0010_1.JPG
      `t_SCF0010_3.JPG
      `t_SCF0010_5.JPG
      `t_SCF0015.JPG
      `t_SCF0016.JPG
      `t_SCF0017.JPG
      `t_SCF0018.JPG
      `t_SCF0019.JPG
      `t_SCF0020.JPG
      `t_SCF0021.JPG
      `t_SCF0022.JPG
      `t_SCF0023.JPG
      `t_SCF0024.JPG

      PS C:\WINDOWS\system32>

      Curtis, again many thanks.

      Regards

      Malcolm J.

  • #67386
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    It would seem you are running into an issue with a particular .zip file. I would probably add some verbose output to just output the name of the current zip file just before it tries to open it so that you can see which one it is failing on.

    IE.

    $verbosepreference = "Continue"
    $ext = "jpg"
    #$parentPath = "L:\JOHN-PC\Backup Set 2017-02-28 160001\Backup Files 2017-03-09 142241"
    $parentPath = "D:\Support"
    Add-Type -Assembly "system.io.compression.filesystem"
    Get-ChildItem "$parentPath\*.zip" -Recurse -File | ForEach-Object {
        Write-Verbose "Attempting to open file: $($_.FullName)"
        $files = [io.compression.zipfile]::OpenRead($_.FullName) | Select-Object -ExpandProperty Entries | Where-Object {$_.Name -like "*.$ext"}
        if ($files) {
            "Zip file: $($_.FullName)"
            $files | ForEach-Object {"`t$($_.Name)"}
        }
    }

You must be logged in to reply to this topic.