Link to home
Start Free TrialLog in
Avatar of Sekans
SekansFlag for United States of America

asked on

ProgressBar to show large file copy progress

Greetings,
I have a procedure that copies a large file (approx. 4MB) from a network server to a local workstation.  The problem I'm having is the user gets impatient and thinks the computer is locked up.  I'd like to use the progress bar to show the status of the file being copied.  My plan was to check the filesize of the file to be copied, assign that to my progressbar.max property.  Then use a timer to check the filesize of the destination file every 1 second.  Then assign that to the progressbar.value property.  Unfortunately, the timer event never fires until the file copy is complete.  Can anyone offer any suggestions.  
Thanks,
Sekans
ASKER CERTIFIED SOLUTION
Avatar of waty
waty
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sekans

ASKER

waty,
I would prefer to use the API.  Please send to bassj@bek.com

I'm curious, how could I rewrite the copy function?

Sekans
Avatar of anthonyc
anthonyc

sekans:

To rewrite a vba function, make a BAS module, and just rewrite the function.  

public sub FileCopy(szSource as string, szDest as string)
  'implement copy here
  msgbox "Guess what, I am going to copy a file now"
  vba.filecopy szSource, szDest
end sub
Avatar of Sekans

ASKER

waty, what do I do with the file you sent me?

Regards,
Sekans
Avatar of Sekans

ASKER

waty:
I don't understand how I can use this to report the progress of a large file copy.  Please ellaborate.

Regards,
Sekans
For waty,
I will also like to see how does it work. Here is my email

dipal_shah@hotmail.com

Please send me the code.

Thanks
Dipal
Using this API, you don't have to use a progress bar. The progress bar is on the window used to copy (coming from the API.

If you still want your own progress bar, you have to rewrite your copy function.

You have a lot of other functions (see in the project I sent you)

To use the API, do as follow :

' *** Delcaration
Private Type SHFILEOPSTRUCT
    hwnd As Long
    wFunc As Long
    pFrom As String
    pTo As String
    fFlags As Integer
    fAnyOperationsAborted As Boolean
    hNameMappings As Long
    lpszProgressTitle As String 'Used only if FOF_SIMPLEPROGRESS specified
End Type

Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long



Sub CopyFile(sFile as string, sDest as string)

    Dim FileOp As SHFILEOPSTRUCT

    FileOp.wFunc = FO_COPY
    FileOp.pFrom = sFile & Chr$(0)
    FileOp.pTo = sDest & Chr$(0)

    If SHFileOperation(FileOp) <> 0 Then
        MsgBox "Did not complete operation successfully."
    End If

End Sub

Hai Waty,
I have used a Progress Bar. But how to use API call. Can you pls. send me the code.
my email is mskannan@sp.ac.sg
Thanks
mskannan
Hi all,
There are a few things you should be aware of when using this function (I don't want to offend anybody with these comments, just give some extra information).

First of all, it doesn't let you specify a user-defined callback function to show the progressbar with. With this function you have only two choices: you use the progress-dialogbox supplied with the explorer or you don't show a thing at all (FOF_SILENT).

The problem with the progress-dialog of Explorer is the "Cancel" button. You cannot get rid of it in a "normal" way, unless you work with more than one thread and let another thread monitor the progressbar (and/or disable the "cancel" button as soon as it appears), but that's very unhandy.

Another problem rises when trying to use pszProgressTitle. On Win95/98 it's just ignored (even when FOF_SIMPLEPROGRESS is specified) and on WinNT you will end up with "cannot read from" access violation and your program will be closed.

Then I haven't even talked about the messageboxes with errors that can rise when using that function. You can suppress them, but that usually means that you have to do something about the error by yourself. Meaning that you must process the returncode of SHFileOperation and (most of the time) call SHFileOperation again, resulting in a loss of valuable time.

If you really need your own progressbar I would suggest that you make a small procedure that cuts the large file in small pieces and copies them one by one. Using an array, a loop and DoEvents you will be able to display a progressbar while copying. The last step, off course, is joining the parts together (or do it at each step - that saves time at the end).
Doing it this way you can have full control, AND you have a function you can use in several other apps as well.

Regards, Abel

PS: When you copy the SHFileOperation function from the API-viewer, make sure you delete the leading space in the Alias-string. It says " SHFileOperationA" and it should be "SHFileOperationA". Otherwise you won't be able to call the function.
Avatar of Sekans

ASKER

waty:
The points are yours.  You supplied a great answer to my question.  Thanks for the help.

Regards,
Sekans