Solved

500pts: File Comparing a directory?

Posted on 2006-11-23
6
186 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
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.

932 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

8 Experts available now in Live!

Get 1:1 Help Now