How to check when Shell32.Folder.CopyHere() is finished
Asked Answered
C

2

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.

Cambridge answered 6/4, 2012 at 11:26 Comment(0)
C
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.

Cambridge answered 6/4, 2012 at 12:30 Comment(3)
+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)Commonage
This solution works on Windows 8 but fire prematurely on Windows 7. Any thoughts?Abaft
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.Rashid
S
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
Subminiature answered 17/5, 2013 at 7:37 Comment(3)
Genius..., should be top answer. This way is probably much more efficient, no blocking kernel calls.Disembark
This is pretty good. However, you may have to adjust for any number of pre-existing items in the folder.Shimmer
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.Rashid
C
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.

Cambridge answered 6/4, 2012 at 12:30 Comment(3)
+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)Commonage
This solution works on Windows 8 but fire prematurely on Windows 7. Any thoughts?Abaft
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.Rashid

© 2022 - 2024 — McMap. All rights reserved.