7

Possible Duplicate:
Delphi notification when a file gets updated

Need monitoring for create files and count them. OS: WinXP and high.

Community
  • 1
  • 1
axissoft
  • 71
  • 1
  • 4

2 Answers2

8

Last year I had the same need and tried out Iztok Kacin's Directory Watch. He responded to email and was very helpful in answering my questions.

His code worked, but I needed to be notified at the moment a file in a specific folder was closed, which for some odd reason, the ReadDirectoryChanges API (on which it depends) from Microsoft (maddeningly) doesn't provide. I also seem to recall that Iztok's code used threads and didn't feel light-weight enough for my needs.

I ended up using a surprisingly simple approach that has worked wonderfully for me. On a TTimer event that fires every few seconds, I use FindFirst on the folder I'm monitoring. All files found are put in a persistent TStringList. Any file that is found that isn't already in the StringList from previous TTimer events is new. (To detect if a file is closed, I try to open the file in exclusive mode. If I can't open it, then it's not added to the TStringList so it's checked on the next event.)

I was quite hesitant to use this approach, thinking it was far too brute-force. But, for the needs I had, this solution has worked out wonderfully and thankfully, involves a small amount of very simple code that is easy to understand and maintain.

HTH

Ville Krumlinde
  • 7,021
  • 1
  • 33
  • 41
RobertFrank
  • 7,332
  • 11
  • 53
  • 99
  • Why did someone vote down my answer above? – RobertFrank Apr 24 '11 at 13:20
  • 2
    Please note there is a small bug in that library. You need to add Terminate in TDirWatchThread.Execute (after SignalError(ErrorMessage)) - Without this it enters an infinite loop when you delete all files from folder then the folder itself ! – Gabriel Apr 22 '17 at 10:30
6

You may want to take a look at this article (A Directory Monitor Class For Delphi), and also at this function from Windows API: ReadDirectoryChanges

You should also take a look at this SO question since it may suit your needs: Delphi notification when a file gets updated

Community
  • 1
  • 1
yms
  • 10,361
  • 3
  • 38
  • 68