Solved

500pts: File Comparing a directory?

Posted on 2006-11-23
6
187 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
  • 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

770 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