[RESOLVED] App not respondning while copying large files
Hey :wave: ,
I've made an application that copies files from a folder to another, and it shows the progress in a textbox. (That's why I have a FileListBox)
Well, while copying large files (2-4 GB) the application 'locks itself' and becomes "Not responding". Is there any way how to prevent this? See code below for more details.
VB Code:
start:
Images = "D:\VMware Images\" ' Path were the backup images are
Destination = "D:\Temp\" & temp1 ' Copy to this folder
File1.Path = Images & temp1 ' Copy from this folder
' If old images exist, delete them and make new folder
If Len(Dir$(Destination, vbDirectory)) > 0 Then
Set fso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFolder Destination, True ' If exist, delete
Set fso = Nothing
MkDir Destination ' And make new
Else
MkDir Destination ' If not exist, make new
End If
' Define number of files to be copied
b = File1.ListCount - 1
' Progress loop starts here
For a = 0 To b
If Text1.Text = "" Then ' Prevents an empty line at the top
Text1.Text = "Copying " & File1.List(a) & "..."
Else
Text1.Text = Text1.Text & vbCrLf & "Copying " & File1.List(a) & "..." ' Text to be shown on the progress-area
End If
Text1.Refresh ' Show the current file in progress-area that is being copied
FileCopy File1.Path & "\" & File1.List(a), Destination & "\" & File1.List(a) ' Defines the Copy from- and the Copy to paths
Next
' Progress loop ends here
The temp1 variable is defined earlier.
Any help is highly appreciated! :)
rade
EDIT: If you didn't understand something, just ask :)
EDIT-2: Even though it becomes "Not responding", it works correctly. When it has finished copying, everything is fine again. I just wonder if you can somehow prevent it to become "Not responding" :)
Re: App not respondning while copying large files
Try using this method. It uses the Windows file copy/move progress dialog.
http://www.vbforums.com/showpost.php...45&postcount=2
Re: App not respondning while copying large files
Thank you RobDog888 !! That seems to work great (don't know yet, have to go down to the 1st floor and test with the comp there.(I'm at 5th floor now ;) )
One further question: Is it possible to force-overwrite the old files? So that this dialog which asks if you're sure you want to overwrite doesn't show up.
Thanks again :)
rade
Re: App not respondning while copying large files
Re: App not respondning while copying large files
Just add the constant to the flags element to overwrite without prompting.
Here are more flags that you may want to know about.
VB Code:
Private Const FOF_NOCONFIRMATION As Long = &H10 'THIS IS THE ONE TO ADD FOR NO WARNING OF OVERWRITE
Private Const FOF_NOCONFIRMMKDIR As Long = &H200
Private Const FOF_NOCOPYSECURITYATTRIBS As Long = &H800
Private Const FOF_NOERRORUI As Long = &H400
Private Const FOF_NORECURSION As Long = &H1000
Private Const FOF_RENAMEONCOLLISION As Long = &H8
Private Const FOF_SILENT As Long = &H4
Private Const FOF_WANTNUKEWARNING As Long = &H4000
'...
.fFlags = FOF_ALLOWUNDO Or FOF_SIMPLEPROGRESS OR FOF_NOCONFIRMATION
Re: App not respondning while copying large files
Quote:
Originally Posted by d3gerald
Have you tested the FileCopy call degerald? I believe it will still peg the cpu during a large copy.
Re: App not respondning while copying large files
Quote:
Originally Posted by RobDog888
Just add the constant to the flags element to overwrite without prompting.
Here are more flags that you may want to know about.
VB Code:
Private Const FOF_NOCONFIRMATION As Long = &H10 'THIS IS THE ONE TO ADD FOR NO WARNING OF OVERWRITE
Private Const FOF_NOCONFIRMMKDIR As Long = &H200
Private Const FOF_NOCOPYSECURITYATTRIBS As Long = &H800
Private Const FOF_NOERRORUI As Long = &H400
Private Const FOF_NORECURSION As Long = &H1000
Private Const FOF_RENAMEONCOLLISION As Long = &H8
Private Const FOF_SILENT As Long = &H4
Private Const FOF_WANTNUKEWARNING As Long = &H4000
'...
.fFlags = FOF_ALLOWUNDO Or FOF_SIMPLEPROGRESS OR FOF_NOCONFIRMATION
Yeah thanks, I just read your next post on the thread you linked to :) Hehe.
I think it work fine now. Will report when I've tested it.
Thank you for all this! :thumb:
rade
Re: App not respondning while copying large files
Hi again,
RobDog888, I tried to do this your way. I'm not sure where to put the code you suggested, but when I put i here, I realized it's inside the loop and it copies the folder as many times as there is files in it. (Because of the Filelistbox loop I have to show the progress in the textbox).
Where should I put your code, so that I still can show the progress?
VB Code:
start:
Images = "D:\VMware Images\" ' Path were the backup images are
Destination = "D:\Temp\" & temp1 ' Copy to this folder
File1.Path = Images & temp1 ' Copy from this folder
With SHFileOp
.wFunc = FO_COPY
.pFrom = Images & temp1 'Select a folder and all its subdirectories/files
.pTo = Destination 'You can also rename the file if you want of leave it the same
.fFlags = FOF_ALLOWUNDO Or FOF_SIMPLEPROGRESS Or FOF_NOCONFIRMATION Or FOF_NOCONFIRMMKDIR
End With
' If old images exist, delete them and make new folder
If Len(Dir$(Destination, vbDirectory)) > 0 Then
Set fso = CreateObject("Scripting.FileSystemObject") ' If exist, delete
fso.DeleteFolder Destination, True
Set fso = Nothing
MkDir Destination ' And make new
Else
MkDir Destination ' If not exist, make new
End If
' Define number of files to be copied
b = File1.ListCount - 1
' Progress loop starts here
For a = 0 To b
If Text1.Text = "" Then ' Prevents an empty line at the top
Text1.Text = "Copying " & File1.List(a) & "..."
Else
Text1.Text = Text1.Text & vbCrLf & "Copying " & File1.List(a) & "..." ' Text to be shown on the progress-area
End If
Text1.Refresh ' Show the current file in progress-area that is being copied
'perform file operation <<<<< Here's your code
SHFileOperation SHFileOp
Next
' Progress loop ends here
rade
Re: App not respondning while copying large files
Try adding the FOF_MULTIDESTFILES const. and I think you pass all the filenames at once.
Private Const FOF_MULTIDESTFILES As Long = &H1
Private Const FOF_FILESONLY As Long = &H80
If your copying all the files in a directory then just specify the folder path and it will do it all.
Re: App not respondning while copying large files
Thanks for that. Will post here tomorrow again when I'm at work (have the project there). :)
rade
Re: App not respondning while copying large files
What? You dont bring your work home with you? Not a hard core programmer :D ;)
Re: App not respondning while copying large files
Quote:
Originally Posted by RobDog888
What? You dont bring your work home with you? Not a hard core programmer :D ;)
At home I have enough work with my own personal projects ;) (that is, VB projects)
Re: App not respondning while copying large files
Yes!
I got i working perfectly. I just put the same CopyFrom and CopyTo variables in the new code, and that whole bunch of new code in the For loop. Now the current file being copied is shown on the Progress-textbox.
VB Code:
' Define number of files to be copied
b = File1.ListCount - 1
' Progress loop starts here
For a = 0 To b
If Text1.Text = "" Then ' Prevents an empty line at the top
Text1.Text = "Copying " & File1.List(a) & "..."
Else
Text1.Text = Text1.Text & vbCrLf & "Copying " & File1.List(a) & "..." ' Text to be shown on the progress-area
End If
Text1.Refresh ' Show the current file in progress-area that is being copied
With SHFileOp
.wFunc = FO_COPY
.pFrom = File1.Path & "\" & File1.List(a) 'Select a folder and all its subdirectories/files
.pTo = Destination & "\" & File1.List(a) 'You can also rename the file if you want of leave it the same
.fFlags = FOF_ALLOWUNDO Or FOF_SIMPLEPROGRESS Or FOF_NOCONFIRMATION Or FOF_NOCONFIRMMKDIR Or FOF_MULTIDESTFILES Or FOF_FILESONLY
End With
'perform file operation
SHFileOperation SHFileOp
Next
' Progress loop ends here
Thank you RobDog888 for all your replies! :)
rade
Re: App not respondning while copying large files
Nice work :)
You can help us, a bit, by going to the Thread Tools menu above the first post and clicking "Mark Thread Resolved". It's just a thing we do :)
Re: App not respondning while copying large files
Quote:
Originally Posted by penagate
Nice work :)
You can help us, a bit, by going to the Thread Tools menu above the first post and clicking "Mark Thread Resolved". It's just a thing we do :)
Oh yes, sorry I just forgot :rolleyes: :)