Solved

Checking Files For Modified Date And Copying

Posted on 2008-10-14
4
1,155 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 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Amazon S3 .Net error 5 43
Why use this lambda? 12 65
JQuery Detecting page post back 6 20
c#, case, if 4 20
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

733 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