Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to monitoring directory for files in Delphi XE? [duplicate]

Possible Duplicate:
Delphi notification when a file gets updated

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

like image 769
axissoft Avatar asked Apr 23 '11 04:04

axissoft


2 Answers

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

like image 89
RobertFrank Avatar answered Nov 03 '22 09:11

RobertFrank


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

like image 37
yms Avatar answered Nov 03 '22 08:11

yms