How to track the directory for files in Delphi XE?

Possible duplicate:
Delphi notification when updating a file

You need to monitor the creation of files and count them. OS: WinXP and high.

+5
file directory delphi monitoring delphi-xe
source share
2 answers

You can take a look at this article (the directory monitor class for Delphi), as well as this function from the Windows API: ReadDirectoryChanges

You should also take a look at this SO question, as it may suit your needs: Delphi notification when updating a file

+5
source share

Last year, I had the same need, and I tried Iztok Kachin: http://www.cromis.net/blog/downloads/directory-watch/ . He answered the email and helped a lot to answer my questions.

Its code worked, but I needed to get a notification at the moment when the file in a certain folder was closed, which for some odd reason, the ReadDirectoryChanges API (on which it depends) does not provide Microsoft (insanely). It also seems to me that Iztok code used streams and did not feel light enough for my needs.

In the end, I used a surprisingly simple approach that worked fine for me. At the TTimer event, which fires every few seconds, I use FindFirst in the folder that I control. All found files are placed in a constant TStringList. A new file that was not found in the StringList from previous TTimer events is new. (To determine if a file is closed, I try to open the file in exclusive mode. If I cannot open it, it will not be added to the TStringList so that it checks the next event.)

I rather did not dare to use this approach, thinking that it was too brute force. But for the needs that I had, this solution turned out great and, fortunately, is associated with a small amount of very simple code that is easy to understand and maintain.

Hth

+4
source share

All Articles