|
-
September 29th, 2005, 09:52 AM
#1
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
-
September 29th, 2005, 10:59 AM
#2
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
-
September 29th, 2005, 11:02 AM
#3
Re: Copying files with FileSystemObject
That's brilliant vb_the_best. Thanks very much. 
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.
Last edited by dhartigan; September 29th, 2005 at 11:11 AM.
-
September 29th, 2005, 11:14 AM
#4
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.
Death is life's special way of telling you you're fired.
For I do not seek to understand in order to believe, but I believe in order to understand. For I believe this: unless I believe, I will not understand. - Anselm of Canterbury (1033–1109)
-
September 29th, 2005, 11:20 AM
#5
Re: Copying files with FileSystemObject
 Originally Posted by dhartigan
That's brilliant vb_the_best. Thanks very much.
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.
-
September 29th, 2005, 02:20 PM
#6
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.
Please remember to rate the posts and threads that you find useful.
How can something be both new and improved at the same time?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|