Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

powershell - extract file type from zip

Tags:

powershell

zip

I have a zipfile with a random filename that is enclosed within a randomly-named zipfile. I am refining the following sample code:

$shell = new-object -com shell.application
 $zip = $shell.NameSpace(“C:\howtogeeksite.zip”)
foreach($item in $zip.items())
 {
 $shell.Namespace(“C:\temp\howtogeek”).copyhere($item)
 }

The outer zipfile contains a few hundred files I don't need plus this one zipfile inside it- How do I refine the above source code to only grab the inner zip file (It could just extract all files from the outer zipfile whose extension is .zip)... Please advise the easiest way to do this. Thanks!

like image 874
user1467163 Avatar asked Mar 26 '26 19:03

user1467163


1 Answers

You need to iterate over the contents checking if the file extension of each item is ".zip" before extracting. Something like this should work:

$shell = new-object -com shell.application
$zip = $shell.NameSpace(“C:\howtogeeksite.zip”)

foreach($item in $zip.items()){

    if([System.IO.Path]::GetExtension($item.Path) -eq ".zip"){

        $shell.Namespace(“C:\temp\howtogeek”).copyhere($item)

    }

}
like image 193
nimizen Avatar answered Mar 28 '26 10:03

nimizen