Solved

500pts: File Comparing a directory?

Posted on 2006-11-23
6
183 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:ianinspain
Comment Utility
feel is supposed to read "file" :-)

0
 
LVL 8

Expert Comment

by:redpipe
Comment Utility
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
Comment Utility
Thank you...

Much appreciated

Ian
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 Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

762 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

11 Experts available now in Live!

Get 1:1 Help Now