Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Checking Files For Modified Date And Copying

Posted on 2008-10-14
4
Medium Priority
?
1,166 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 1500 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

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

571 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