2

I want to watch all changes in a directory recursively, i.e. if watching C:\ I want to receive notifications for

C:\1.txt
C:\A\1.txt

If I use ReadDirectoryChanges with watchSubtree option enabled, it reports only

C:\1.txt
C:\A

It does not work recursively.

How can I make it work recursively? Do I really need to call ReadDirectoryChangesfor each subdirectory recusively? What if new subdirectories are created / subdirectories get deleted?

Thanks for any help.

jpfollenius
  • 16,456
  • 10
  • 90
  • 156
  • 2
    @Smasher, the `ReadDirectoryChanges` function can works recursively, maybe the problem is in your implementation of this function.please share the code which you are using in order to get help. – RRUZ May 30 '11 at 15:14
  • @RRUZ: I would be more than happy if you are right, but I don't think so. I'm using the code posted by @mghie in this answer: http://stackoverflow.com/questions/863135/why-does-readdirectorychangesw-omit-events/864050#864050 He mentions this fact as well. Also this blog article http://qualapps.blogspot.com/2010/05/understanding-readdirectorychangesw.html states it, but without a solution as well. – jpfollenius May 30 '11 at 15:22
  • @RRUZ: just to be clear: it DOES report the change of `C:\A\1.txt` but the file name in the notification is `C:\A` and not `C:\A\1.txt`. – jpfollenius May 30 '11 at 15:24
  • 1
    I see nothing in those articles about the bWatchSubtree parameter not working, except that the Stack Overflow answer mentions a caveat about *deletion* of files and how you won't be notified about every single file. Please give more details about what doesn't work and what you were expecting. Also note the recommendations *against* using this API to monitor changes to an entire drive. Use the change journal for that. – Rob Kennedy May 30 '11 at 15:43
  • @Smasher try this implementation http://www.koders.com/delphi/fidD3135FB08B930CFAC5A92ECBA3D67C83852488B2.aspx?s=zip#L30, i just tested and works ok (this means which report the full name of the file modified), just need a few changes to omit warnings in delphi XE. just a tip, in the past i have problems (some notifications was lost) when I trying to monitor the system root folder recursively using the `ReadDirectoryChanges` function. check this sample image which shows the full filename in the notifications http://dl.dropbox.com/u/12733424/MonitorDemo.png – RRUZ May 30 '11 at 16:02
  • @Rob: "mentions a caveat about deletion of files and how you won't be notified about every single file" That's exactly what I mean. I need a notification for every single file. I don't plan to use this on a whole drive. It was just an (not very well-chosen) example. – jpfollenius May 30 '11 at 16:15
  • @RRUZ: thanks for the link and your help. I'll try this when I get back to work tomorrow. – jpfollenius May 30 '11 at 16:16
  • You can also look at my component that does the same: http://www.cromis.net/blog/downloads/directory-watch/ It works ok for me in production code. – Runner May 30 '11 at 17:48
  • Thank you all! I got it working. If you post your links as an answer, I'll accept it. – jpfollenius May 31 '11 at 07:04
  • @Runner: Both helped :) I read both and then rewrote parts of my own code, which made it work. I probably took more code out of your link though. – jpfollenius May 31 '11 at 07:59

2 Answers2

2

You can look at DirectoryWatch component which works. I use it in production code so I know it works correctly, even for subdirs.

Runner
  • 6,073
  • 26
  • 38
  • I accept this because it helped me to get it right. Thanks to @RRUZ for the good link as well. BTW: do you handle the buffer overflow case in your code? – jpfollenius May 31 '11 at 08:43
1

You probably want change journals if you're trying to watch the whole disk.

Adrian McCarthy
  • 45,555
  • 16
  • 123
  • 175