1

I apologize if this has been addressed before, but I've been unsuccessful in searching online for this answer.

We have a situation where we're syncing a local file to a network file, as per dummied down code below. E.g., if the network file exists, copy it to the local drive. If the network file is non-existent, then delete it from the local drive.

My question is, do I require some delay/test between the Kill line and the Filecopy line? So far, testing hasn't turned up any problem, but I'm still a bit unsure if this is sufficient.

Sub copyFile()

Const SFile As String = "N:\Test.txt"
Const tFile As String = "C:\Test.txt"

On Error Resume Next

Kill tFile

On Error GoTo errTrap

FileCopy SFile, tFile
errTrap:
End Sub
DaveU
  • 1,082
  • 2
  • 14
  • 25
  • 1
    "Kill" deletes the file and does not continue until the file is removed. The only issue to worry about is if the file does not exist when a Kill is used. "On Error" will work, or you can first check if the file exists before using that command. Personally I try and avoid using On Error unless there is no other choice. – Keith Swerling Jul 26 '22 at 18:24

1 Answers1

2

I think your code is good, you can add error handler, when FileCopy error.

Sub copyFile()

Const SFile As String = "N:\Test.txt"
Const tFile As String = "C:\Test.txt"

On Error Resume Next

Kill tFile

On Error GoTo errTrap

FileCopy SFile, tFile
exit sub

errTrap:
'do something when FileCopy Error

End Sub
Eric K.
  • 814
  • 2
  • 13
  • 22
  • Thanks for your reply.. My actual concern was the possibility of the VBA code getting ahead of the OS, that is, trying to copy a file before the original has been completely deleted.. The code as shown has been simplified to illustrate this, hence the lack of error handling. – DaveU Dec 04 '15 at 06:07
  • It should not be a problem, or you can try this [link](http://stackoverflow.com/questions/16943003/vba-to-copy-a-file-from-one-directory-to-another) – Eric K. Dec 04 '15 at 06:31
  • 1
    Eric, you didn't really tell me anything I didn't already know, I was waiting for a more "in depth" answer that would get into timing issues between VBA and the OS. Since none seem to be forthcoming, I'll check your answer. Please note, this comment is not meant in a belittling tone, I really did appreciate your input. Thanks. – DaveU Dec 13 '15 at 06:19