?
Solved

Checking Files For Modified Date And Copying

Posted on 2008-10-14
4
Medium Priority
?
1,158 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

801 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