7

I need to unzip en zip some files in my application using Shell32. Right now, I use srcFolder.CopyHere(destFolder.Items()) to achieve this. However, my next line of code requires the newly made ZIP-file. But since the CopyHere method is Async, how can I check when it in finished? Right now I use a Thread.Sleep for around 500 ms which is enough for my computer to finish creating the ZIP file, but it's not good code imo.

Any ideas?

More info/code can be provided if necessary.

Jelle Capenberghs
  • 764
  • 2
  • 13
  • 30

2 Answers2

10

Here is another one for waiting to finish copying

'Wait until the File/Folder has finished writing to zip file...
Do Until Shell.NameSpace(Filename).Items.Count = Shell.NameSpace(Input).Items.Count
    System.Threading.Thread.Sleep(200)
    System.Diagnostics.Debug.Print("Waiting...")
Loop
Fid
  • 146
  • 2
  • 8
  • Genius..., should be top answer. This way is probably much more efficient, no blocking kernel calls. – Motes Dec 04 '13 at 21:20
  • This is pretty good. However, you may have to adjust for any number of pre-existing items in the folder. – k rey Dec 16 '13 at 15:00
  • Thiss approach still sometimes throws errors. We have implemented this in our project but in one of our client machines this raises Error 70 or Error 75. – sidnc86 Jun 20 '17 at 10:09
6

I found it, used something like this:

  srcFolder.CopyHere(destFolder.Items())


            While FileInUse(FILEPATH & "BudgetJaarOverzichtSMB.zip")
                Thread.Sleep(100)
            End While

    Private Function FileInUse(ByVal FilePath As String) As Boolean
        Try
            FileOpen(1, FilePath, OpenMode.Input)
            FileClose(1)
            Return False    ' File not in use
        Catch ex As Exception
            Return True     ' File in use
        End Try
    End Function

Not really perfect but will lose less time than with my first approach.

Jelle Capenberghs
  • 764
  • 2
  • 13
  • 30
  • +1 Tried to find something, but found anything. Just a note. Add some safety measure to break that loop if your copy takes too long. (For example unavailable network paths) – Steve Apr 06 '12 at 13:30
  • This solution works on Windows 8 but fire prematurely on Windows 7. Any thoughts? – Dan Gifford Jun 12 '15 at 16:43
  • Using Excel VBA, we need to 1. Copy a text file to zip 2. Get the zip's byte stream for further processing. 3. Delete text and zip files. As the code is giving us unusual and irregular errors (always related to File paths/zip file paths), we extracted code for 1 and 3 into another test environment and reran it multiple number of times. Here we implemented FileInUse solution but somehow the function keeps on returning false and deletes don't execute at all. – sidnc86 Jun 20 '17 at 10:22