CopyFile TIP

General Macro Scheduler discussion

Moderators: Dorian (MJT support), JRL

Post Reply
User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

CopyFile TIP

Post by Bob Hansen » Tue Jan 21, 2003 12:18 am

I recently found out (probably knew it before, and forgot it :oops:) that it is possible to lose a file when doing CopyFile.

I had done Let>CF_OVERWRITE=1, and then did a CopyFile>Source,Destination. I had not done any checks for copy success and later I was having some random problems based on the destination file. :?

Working back I found that sometimes the Destination file did not exist. Going back further, I found that it did not exist because the Source file was in use and the CopyFile did not copy to destination. :?

But the "old" Destination file had already been deleted. :!: Result was that I lost the "old" file, did not get a copy of the "new" file, and trouble followed.

:idea: Simplified solution:
===============================
//Rename old copy to keep for restoration if needed
MoveFile>old,temp

CopyFile>new, old
IfFileExists>old,Good

Message>Error copying new file to old
//Keep original "old" file that was deleted when CopyFile started
MoveFile>temp,old
GoTo>ErrorRoutine

Label>Good
//Remove original "old" file, not needed any longer
DeleteFile>temp
Goto>End

Label>ErrorRoutine
.....
.....
.....

Label>End

==================================
The simplified script above only does a check on the CopyFile instruction, but a verification could be done after each MoveFile, CopyFile, DeleteFile instruction.

I haven't done any test at this point to see what happens when CF_OVERWRITE is at default=0. I do not think this problem will occur. But if you do use CF_OVERWRITE=1, you should take the extra steps to copy the destination file until you are sure that the replacement file has been copied successfully.

Now that this has been written out, it seems obvious to me that this would happen. (Also need to follow up on the recent messages in the forum on identifying locked files).

As a general practice, all copy/write/delete routines should be verified, but those extra programming steps never seem important enough to put in the extra effort/time.

:idea: Hope this is a helpful TIP to someone else. :arrow:
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts