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
dkmackenzieAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SimpsonThePhilCommented:
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.

Phil
0
VBClassicGuyCommented:
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
Hell:
   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
Wend
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.