views:

19

answers:

1

Having trouble with WSH and Windows Compression.

My goal is to be able to zip up files (not folders, but individual files from various locations, which I have stored in an array) using the built-in Windows Compression. I am using VB6.

Here is my routine (vb6 code):

Dim objShell
Dim objFolder
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.namespace(savePath & "\export.zip")
' --
' loop through array holding files to zip
For i = 0 To filePointer
  objFolder.CopyHere (filesToZip(i))
Next
' --
Set objShell = Nothing
Set objFolder = Nothing

It works, but issues arise when there are more than a few files. I start getting errors from Windows (presumably, its calling the compression too fast, and the zip file is locked). I cant seem to figure out how to WAIT until the COPYHERE function completes before calling the next one to avoid issues.

Does anyone have any experience with this?

Thanks -

+1  A: 

You should be able to achieve that sort of synchronization by checking the file count in your target ZIP folder before proceeding to the next loop iteration (as suggested here and here):

For i = 0 To filePointer
  objFolder.CopyHere filesToZip(i)

  Do Until objFolder.Items.Count = i+1
    WScript.Sleep 100
  Loop
Next
Helen
Helen- works on my single-core PC. On my quad-core, it doesn't. Sound strange? Anything I can do for that (maybe the multiple cores throws off the logic)? Thanks.
OneNerd
@OneNerd: Do you get Windows errors or VBScript runtime errors? If the latter, which error do you get, on which line etc?
Helen
well, it takes about 1 minute for each file to zip - its like its hanging or something. Not sure - I will tinker some more - gave you a vote up, and if I can get it to work, will mark as accepted. Thanks.
OneNerd
wound up going another way, but yours did work on my single-core processor, so marking as accepted. thanks Helen!
OneNerd