0

I can't quite remember where I found the answer that led me to use this Powershell snippet, but I've been using this script to zip a folder and the files contained within it:

param (
[string]$ZipFrom,
[string]$ZipTo
)
function Add-Zip
{
    param([string]$zipfilename)

    if(-not (test-path($zipfilename)))
    {
        set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
    (dir $zipfilename).IsReadOnly = $false  
    }

    $shellApplication = new-object -com shell.application
    $zipPackage = $shellApplication.NameSpace($zipfilename)

    foreach($file in $input) 
    { 
        $zipPackage.CopyHere($file.FullName)
        Start-sleep -milliseconds 500
    }
}

If(Test-path $ZipTo) {Remove-item $ZipTo}

dir $ZipFrom -Recurse | Add-Zip $ZipTo

Not sure what I'm missing, but it seems like it's zipping HALF of the files within the folder. I just tried this on a folder with 62 files in it, and I ended up with a zip archive with 31 files in it.

Jeffrey Van Laethem
  • 2,601
  • 1
  • 20
  • 30

1 Answers1

0

You're experiencing the same issue that others have noticed when using the Shell.Application's CopyHere() command. It's asynchronous (non-blocking) and doesn't report completed events.

Adding a Sleep() won't work reliably for large files because the Shell.Application object will be disposed and disconnect from its child threads before they've completed.

Try repeatedly checking for the existence of the destination file like in the linked answer. Hope this helps!

Rich Moss
  • 2,195
  • 1
  • 13
  • 18