powershell - Only copy certain file types after unzipping -
i have following code unzip many folders in directory tree:
new-item e:\files -type directory get-childitem -path e:\snl_insurance\* -recurse -exclude "*.md5" | foreach-object { $file = $_ write-host $file; $destination = "e:\files" $shell = new-object -com shell.application $zip = $shell.namespace($file.fullname) foreach($item in $zip.items()){ if ($item.name -eq ".txt") { $shell.namespace($destination).copyhere($item) } } }
without if statement script copies zip files on too, not text files contained beneath. thought check make sure file extension .txt (or *.txt ive tried both) $item.name doesn't seem contain thought did. if have ideas and/or can explain $shell variable here (and $shell.namespace helpful) in advance.
edit: responses. found way before saw these answers. here solution found if interested:
new-item e:\files -type directory get-childitem -path e:\snl_insurance\insurance\* -recurse -exclude "*.md5"| foreach-object { if ($_.extension -eq ".zip") { get-childitem -path $_ -recurse| foreach-object { $file = $_ write-host $file; $destination = "e:\files" $shell = new-object -com shell.application $zip = $shell.namespace($file.fullname) foreach($item in $zip.items()){ $shell.namespace($destination).copyhere($item) } } } }
to copy text files can use:
foreach($item in $zip.items()){ if ($item.type -eq 'text document') { $shell.namespace($destination).copyhere($item) } }
or
$zip.items() | where-object {$_.type -eq 'text document'} | foreach-object { $shell.namespace($destination).copyhere($_)}
how did know use type
property? used:
$zip.items()
to properties available use.
Comments
Post a Comment