• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 437
  • Last Modified:

Handling a filecopy error in VB.

I am new to VB.  I am using filecopy to copy thousands of files from one place to another from an input list supplied by another program.  On an error (like the source file has unspoorted characters - don't ask, or the file can't be found) the code stops.  I want the code to just skip the operation on error and just continue to the next one.  Also, maybe save the line from the input file that caused error.

the code is extremely basic:

FileCopy file1, file2
1 Solution
Put the code that does the individual file copy in a function which has an on error goto statement, get the function to return true or false depending on sucess.  Create another sub that walks through the filelist calling the function for each file.

This way you can isolate the actual individual copy function and put what ever error trapping in there, maybe you write the filename of the bad copy to an error file etc.

For a quick and dirty solution that doesn't save the line that caused the error, just put "On Local Error Resume Next" at the top of your routine that copies all the files. Or, for the whole ball of wax, use:
Private Sub CopyTheFiles()
   Dim source As String, target As String, txt As String
   On Local Error Goto Hell
   source = file1
   target = file2
   FileCopy source, target
   source = file3
   target = file4
   FileCopy source, target
   'add as many "source =", target =", and FileCopy statements as needed
   Exit Sub
   txt = source & " " & target   'do whatever with this line...display, save to a file, etc.
   Resume Next
End Sub
Since you're reading the source and target files from an external file, a better way is to open the file list, and read each file set, set source and target accordingly, do the copy, and get the next pair until you reach the end of file which holds what files to copy. Something like:
While Not EOF(1)   'this is working on the file that contains the list of files to copy
   Line Input #1, txt
   'parse string here to set source and target variables
   FileCopy source, target
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now