dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Copying files with FileSystemObject

  1. #1
    Join Date
    Jan 2005
    Posts
    77

    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

  2. #2
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    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

  3. #3
    Join Date
    Jan 2005
    Posts
    77

    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.

  4. #4
    Join Date
    Jan 2004
    Location
    San Diego
    Posts
    148

    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 (10331109)

  5. #5
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Copying files with FileSystemObject

    Quote 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.

  6. #6
    Join Date
    Dec 2001
    Posts
    6,332

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)