Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if a Directory/Folder has been changed

I have a folder with the following structure

Parent/
    Child1/
        GrandChild1/
             File1.txt

I need to query Parent folder and find out if Child1 has changed.

Changed = A new file was add/update/deleted.

The Child1 folder DateModified is not updated. Only the GrandChild1 date modified was updated when changes occurs. I am trying to avoid going to the file level to determine if the rootparent has changed. since there will be many folders and sub folder. I just need to know if Child1 has changed.

I do not want to use FileSystemWatcher, since I am running this as a scheduled job and not watching it LIVE.

like image 203
Arcadian Avatar asked Feb 19 '23 21:02

Arcadian


1 Answers

User FileSystemWatcher. Remember to enable raising events since it is a common mistake (watchfolder.EnableRaisingEvents = true;).

The FileSystemWatcher may prove not to be optimal from a performance perspective. If that is an issue for you, you might implement a CRC check with a Timer to check for changes of the files and folders you are interested in.

Essentially, what I would do is to generate a CRC32 hash for the entire folder I am watching (and save it away into variable A) and when I decide it is time to check for changes, you simply calculate a new CRC32 hash for the same folder (into variable B). You then compare A with B and if they don´t match, something has changed. Really not that difficult.

Reference:

  • http://www.codeproject.com/Articles/26528/C-Application-to-Watch-a-File-or-Directory-using-F
  • http://social.msdn.microsoft.com/Forums/zh/netfxbcl/thread/b7612249-eb32-4005-9d6b-7f291c218326
  • http://damieng.com/blog/2006/08/08/calculating_crc32_in_c_and_net
  • http://marknelson.us/1992/05/01/file-verification-using-crc-2/
like image 133
Marcus Avatar answered Mar 06 '23 00:03

Marcus