powershell - How might I update this code to run a second time but only on a zip archive already unzipped? -
here code using:
# don't include "\" @ end of $newsource - stop script # matching first-level subfolders $ignore = "somename" $files = gci $newsource -recurse | { $_.extension -match "zip||prd" -and $_.fullname -notlike $ignore } foreach ($file in $files) { $newsource = $file.fullname # join-path standard powershell cmdlet $destination = join-path (split-path -parent $file.fullname) $file.basename write-host -fore green $destination $destination = "-o" + $destination # start-process needs path exe , arguments passed # separately. can add -wait have process complete before # moving next start-process -filepath "c:\program files\7-zip\7z.exe" -argumentlist "x -y $newsource $destination" -wait }
however, once finished need go through new directories , unzip .prd files created after unzipping .zip archives. need here tries aren't working , unzip , overwrite unzipped .prd , .zip files.
i already told you $_.extension -match "zip||prd"
matches all extensions, because of empty string between 2 |
characters in regular expression (all strings contain empty string).
also, -notlike
, -like
operators behave -ne
, -eq
operators when comparing value pattern doesn't have wildcards in it, second condition match files full name isn't exactly "somename".
change this:
$ignore = "somename" $files = gci $newsource -recurse | { $_.extension -match "zip||prd" -and $_.fullname -notlike $ignore }
into this:
$ignore = "*somename*" $files = gci $newsource -recurse | { $_.extension -match "zip|prd" -and $_.fullname -notlike $ignore }
and code should expect.
as alternative build list of paths want ignore
$ignore = 'c:\path\to\first.zip', 'c:\other\path\to\second.zip', 'c:\some\file.prd', ...
and use -notin
(powershell v3 or newer) or -notcontains
operator exclude files:
$_.fullname -notin $ignore $ignore -notcontains $_.fullname
as side note, i'd use call operator , splatting instead of start-process
invoking 7zip.exe
:
$destination = join-path (split-path -parent $file.fullname) $file.basename $params = 'x', '-y', $newsource, "-o$destination" & "${env:programfiles}\7-zip\7z.exe" @params
to extract .prd files extracted zip archives add step loop.
foreach ($file in $files) { ... & "${env:programfiles}\7-zip\7z.exe" @params get-childitem $destination | where-object { $_.extension -eq 'prd' } | foreach-object { # extract matching file here, procedure # same files in outer loop } }
you may want wrap code building destination path , extracting file in function reads paths from pipeline , calls recursively if destination path contains .prd files.
function invoke-unzip { [cmdletbinding()] param( [parameter( mandatory=$true, valuefrompipeline=$true, valuefrompipelinebypropertyname=$true )] [validatescript({test-path -literalpath $_})] [string]$fullname ) $newsource = $fullname ... & "${env:programfiles}\7-zip\7z.exe" @params get-childitem $destination | where-object { $_.extension -eq 'prd' } | invoke-unzip }
Comments
Post a Comment