Solved

Checking Files For Modified Date And Copying

Posted on 2008-10-14
4
1,151 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
Comment Utility
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
Comment Utility
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
Comment Utility
0
 
LVL 5

Accepted Solution

by:
jfmador earned 500 total points
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

15 Experts available now in Live!

Get 1:1 Help Now