Solved

500pts: File Comparing a directory?

Posted on 2006-11-23
6
189 Views
Last Modified: 2010-04-16
Hi there,

I wonder if anyone can help?

I am trying to find the easiest way to file compare a complete directory.. The reason for this is to find out if i need to send any changes in the directory to a Database BLOB field..

I have the blob field working... What i need to do is to take a Snapshot of all files when I enter my program... I thought of reading in the files into a array or something but a little unsure here...

During the use of the program files in a certain directroy may get updated or files maybe added or deleted.... so when i exit the program i will compare my previous Snapshot to the files now in the directory and if any have changed then i would send these files to the db.... or remove files from the db or add files to the db.....

Any help or info on this would be really helpful, i am a little lost the best way of doing this... If i keep the files in an array in memory this would be costly wouldn't it??

Thanks in advance

Ian
0
Comment
Question by:ianinspain
[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
  • 3
  • 2
6 Comments
 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 200 total points
ID: 18001775
Did you think about using FileSystemWatcher? Something like this:

http://www.codeproject.com/cs/files/directorymirror.asp
0
 
LVL 8

Accepted Solution

by:
redpipe earned 300 total points
ID: 18002772
The FileSystemWatcher is very good when you need to trigger on events, as they happen. If you only want to compare two different snapshots, I would used a simple hashtable.

    public System.Collections.Hashtable Init(string path)
    {
      System.Collections.Hashtable ht = new System.Collections.Hashtable();
      System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(path);
      foreach (System.IO.FileInfo fi in di.GetFiles()) {
        ht.Add(fi.Name, fi.LastWriteTime);
      }
    }

    public void Compare(string path, System.Collections.Hashtable htOld)
    {
      System.Collections.Hashtable htCurrent = new System.Collections.Hashtable();
      System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(path);
      foreach (System.IO.FileInfo fi in di.GetFiles()) {
        if (!htOld.ContainsKey(fi.Name)) {//new file
          //TODO: Add code to add current file to database
        }
      }
      foreach (System.Collections.DictionaryEntry de in htOld.Keys) {
        System.IO.FileInfo fi = new System.IO.FileInfo(path + "\\" + de.Key.ToString());
        if (fi.Exists) {
          if (fi.LastWriteTime > Convert.ToDateTime(de.Value)) {
            //TODO: Add code to update current file to database
          }
        } else { //deleted file
          //TODO: Add code to remove current file from database
        }
      }
    }
0
 

Author Comment

by:ianinspain
ID: 18003647
hi redpipe, this i like... but i was wondering about the property

LastWriteTime

is this guaranteed to be always updated when a feel is changed??

Just wondering, otherwise it sounds a really good solution..

AlexFM, this is also a brilliant solution, i will probably keep this and implement elsewhere.... i never even knew it existed... thnaks for that

Ian
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ianinspain
ID: 18004016
feel is supposed to read "file" :-)

0
 
LVL 8

Expert Comment

by:redpipe
ID: 18006289
hi Ian

The "LastWriteTime" property is the same as the "Date Modified" column in Windows Explorer. As all properties with a getter and setter it is possible to programatically override the "correct" value (e.g. set the LastWriteTime to a date before the computer was invented :-), but if you just write your changes to the file through the normal System.IO methods, the property should be updated accordingly by the OS.
0
 

Author Comment

by:ianinspain
ID: 18029609
Thank you...

Much appreciated

Ian
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
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 antispam), the admini…

726 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