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.
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.
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.
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.
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
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.