Solved

Checking Files For Modified Date And Copying

Posted on 2008-10-14
4
1,153 Views
Last Modified: 2013-12-17
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)
{
      File.Copy(myFileInDirect2.FullName.ToString(), pathOfbackupDirectory);
}

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.
0
Comment
Question by:angus_young_acdc
  • 2
4 Comments
 
LVL 12

Expert Comment

by:ShazbotOK
ID: 22710696
Is this running as a window's service?  If so use FileSystemWatcher instead... this will allow you to react on an 'event' basis "OnFileChanged" to run your comparison logic with the backup directory.
 
0
 
LVL 15

Author Comment

by:angus_young_acdc
ID: 22710832
No it's not running as a service.

Although having said that it may be better to do it as that.   Would you have any info on how to create such a service (not the standard service creating tutorials, but hopefully something similar to the query I have)?
0
 
LVL 12

Expert Comment

by:ShazbotOK
ID: 22717032
0
 
LVL 5

Accepted Solution

by:
jfmador earned 500 total points
ID: 22717818
There is three problems about using the FileSystemWatcher,

First one is that you will need to do a copy of your file before the first use of your service, because you will receive an event only for the new or modified file, then it won't copy your files by itself on the first use.
Second is that if your service failed or is stopped you will miss some modified files for you backup.
Third, if you have many file created on your hard drive in your folder, the FileSystemWatcher will copy them in your backup folder at the same time than the file are created in your original folder, then it will slow down your file creation/copy. Using batch copy you can prevent this issue by copying your file overnight.

I would also suggest you to take a look to an utility from Microsoft Robocopy, it will do exactly what you want, there is a command line and a gui version of this tool, you can use it from your application or run it from your task scheduler

Gui Version
http://technet.microsoft.com/en-us/magazine/cc160891.aspx

Command version usage
http://technet.microsoft.com/en-us/library/cc733145.aspx
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now