I have an extremely large volume of files that need to be individually ZIPed (in blocks of 200-400k files). While using the function below, sometimes the file does not get added to the ZIP file (initialized/created as part of 'new-object') and the function gets stuck in an infinite loop checking for the pre-created ZIP file to grow in size greater than its initialized size of 24 bytes. All files I'm compressing are less than 200-300k in size and compress immediately. If the addition to the ZIP file takes longer than 50ms, I see an error pop up once and then it moves onto the next file because the second check for file growth becomes true and the file add has successfully completed.
Personally I just would like to add a counter that checks to see how many times the while statement gets executed so that after 5-10 times, it re-issues the the command to add the file to the zipfile (which appears to resolve the issue when executed manually from a seperate powershell window).
One option that came to mind was to wrap line for adding the file (into the ZIP file) into the while clause and then try to incorporate the counter in there. I just haven't tried it yet.
Any additional thoughts on how to accomplish the above modification (to the script below) would be appreciated as my biggest issue is the inability to re-create the problem at-will.
function zipit {
Param([string]$path)
if (-not $path.EndsWith('.zip')) {$path += '.zip'}
if (-not (test-path $path)) {
set-content $path ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
}
$ZipFile = (new-object -com shell.application).NameSpace($path)
$input | foreach {$zipfile.CopyHere($_.fullname); while ((Get-Item $path).length -le 25) {Start-Sleep -m 50}}
}
Any help is much appreciated.
- Rob
- Edited by MNPolarbear 12 hours 34 minutes ago