Checking Files For Modified Date And Copying
Posted on 2008-10-14
I'm writing a small app to copy files from one hard drive location to another (for backup purposes).
My code works fine to check both directories, see if the files, check the last modified date and if different copy the newest one to the other directory.
Where I am stuck is as follows:
1: I'm using a foreach loop (twice) to check each file in Directory1 (original), and then within that another foreach loop to check the files in Directory2 (backup). This takes the first file found and then compares the LastWriteTime, and will copy. However should the file in Directory1 not exist in Directory2 it will be comparing differeing files. Would it be safe to say:
if(myFileInDirect1.Name != myFileInDirect2)
So that it basically will copy that file to the backup folder?
2: If the above wasn't a safe method, what would be the best way to do it? The files need to be checked to see if the original has been updated, then copy that (and overwrite) the backup in another directory.