Copying files with FileSystemObject
Hi,
Can anyone tell me how I can do the following:
I need to copy a file from one location on my hard drive to another. I want to be able to overwrite existing files and be able to trap any errors that occur.
The code I use is as follows:
Code:
Dim fso as New FileSystemObject
Dim sSource as String
Dim sTarget as String
fso.CopyFile sSource, sTarget
By default this will overwrite existing files, but how can I check if there are errors?
Also, what are the other alternatives to using FileSystemObject to copy files, and which is the *best* way to do it?
Thanks very much,
dhartigan
Re: Copying files with FileSystemObject
Put an Inline Error Handler to check if there were any errors. something like this
Code:
On Error Resume Next
fso.CopyFile sSource, sTarget
If Err.Number <> 0 Then
'There was an error, so handle it here
Msgbox Err.Description
Else
'There was no error
End If
Err.Clear
You could also use SHFileOperation API to Copy the files and trap any errors if you want. Refer your API Reference
Re: Copying files with FileSystemObject
That's brilliant vb_the_best. Thanks very much. :thumb:
dhartigan
P.S I can't rate your post yet because I have to "spread some reputation around first", but I will rate it when I can.
Re: Copying files with FileSystemObject
The FileSystemObject CopyFile Method performs the same as your standard copy and paste in the explorer environment except the warning messages act as fatal errors. There isn't any rollback or recovery from these errors as far as I know. You can try capturing the error codes with an On Error statement. Number from the Err object will give you the error code and Description gives you the text of the error. You can also find the object or application that generated the error with the Source method. e.g.
Code:
Dim fso as New FileSystemObject
Dim sSource as String
Dim sTarget as String
On Error Goto ErrHandler
fso.CopyFile sSource, sTarget
ErrHandler:
MsgBox "Error #: " & Err.Number & vbNewLine & "Error Text: " & Err.Description & vbNewline & "Error generated by: " & Err.Source
You can use the On Error to write code to recover from and/or handle errors with an If..Else or Select Case conditional structure using the error codes generated. You may have to play around with this a little to find the exact error codes you want to recover from or record. Once an error is generated, unless you restart/resume your code from the point the error was generated, the routine or function you're running will stop.
Re: Copying files with FileSystemObject
Quote:
Originally Posted by dhartigan
That's brilliant vb_the_best. Thanks very much. :thumb:
dhartigan
P.S I can't rate your post yet because I have to "spread some reputation around first", but I will rate it when I can.
I am glad that it helped. Rating is not that important as saying Thank You. You said it and I am happy and satisfied.:wave:
Re: Copying files with FileSystemObject
Well, you can also use FileCopy, which is built-in to vb, so you don't need the extra overhead of the FSO. I don't recall if it overwrites, but for that you can use Kill to delete files if needed.
I haven't needed the FSO except for vbscript.