Author Posts

November 7, 2016 at 1:59 pm

Hello All,

I haven't quite got the hang of how to skip a directory or two while using the following code:

#-match is a regex, so you can do || which is OR
#Using the OR || switch to unzip all files with .zip or .prd
$files=gci $ScriptPath -recurse | where {$_.Extension -match "zip||prd"}
foreach ($file in $files) {
    $source = $file.FullName
	#Join-Path is a standard Powershell cmdLet
    $destination = Join-Path (Split-Path -parent $file.FullName) $file.BaseName
    write-host -fore green $destination
    $destination = "-o" + $destination
    #Start-Process needs the path to the exe and then the arguments passed seperately. You
    #can also add -wait to have the process complete before moving to the next
    Start-Process -FilePath "C:\Program Files\7-Zip\7z.exe" -ArgumentList "x -y $source $destination" -Wait
}

Right now, I'm having an issue with this section of code waiting for input for some zip archives that wants a password to extract. However, I simply do not need to extract the files in a directory called "Reports."

November 7, 2016 at 3:14 pm

You could use an "if" block to skip over it, or just filter them out in your where clause on line 3.

November 7, 2016 at 4:46 pm

Is it best to use:

Get-ChildItem -File *.prd, *.zip -Recurse |`
Rename-Item -NewName { $_.name -replace '\.(prd|zip)$','_$1.$1' -inotmatch "CustDataDir","MyDataDir"} 

or

Get-ChildItem -File *.prd, *.zip -Recurse |`
Rename-Item -NewName { $_.name -replace '\.(prd|zip)$','_$1.$1' -inotmatch "($)$ScriptPath\CustDataDir","($)$ScriptPath\MyDataDir"} 

or

Get-ChildItem -File *.prd, *.zip -Recurse -Exclude *CustDataDir*,*MyDataDir* |`
 Rename-Item -NewName { $_.name -replace '\.(prd|zip)$','_$1.$1'} 

or

Get-ChildItem -File *.prd, *.zip -Recurse -Exclude '$ScriptPath\CustDataDir',$ScriptPath\MyDataDir* | `
Rename-Item -NewName { $_.name -replace '\.(prd|zip)$','_$1.$1'} 

or

Your better solution?