• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 234
  • Last Modified:

Monitoring a directory (NT 4) for new files

I use Delphi 4. Iam writing an application that needs to monitor a directory for new files (or directories) that are created in it.
What is the easiest way of doing this?
0
PhilZ
Asked:
PhilZ
  • 3
1 Solution
 
intheCommented:
hi,
this is example of FindfirstFileNotification()


unit FileNTU;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls, Buttons, FileCtrl, Menus, ComCtrls;

type
  TForm1 = class(TForm)
    DriveComboBox1: TDriveComboBox;
    DirectoryListBox1: TDirectoryListBox;
    FileListBox1: TFileListBox;
    StatusBar1: TStatusBar;
    Button1: TButton;
    Memo1: TMemo;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  protected
  end;

var
  Form1: TForm1;
  NotificationHandle: THandle;    // holds the handle to the notification object

implementation

{$R *.DFM}

procedure TForm1.Button2Click(Sender: TObject);
begin
  {establish a notification for file name changes on the selected directory}
  NotificationHandle := FindFirstChangeNotification(PChar(DirectoryListBox1.
                                                  Directory), FALSE,
                                                  FILE_NOTIFY_CHANGE_FILE_NAME);

  {if the notification was set up correctly, modify some UI elements...}
  if (NotificationHandle <> INVALID_HANDLE_VALUE) then
  begin
    Button1.Enabled := TRUE;
    Button2.Enabled := FALSE;
  end
  else
  begin
    {...otherwise indicate that there was an error}
    ShowMessage('There was an error setting the notification');
    Exit;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  dwResult: DWORD;         // holds the result of waiting on the notification
  Waiting: Boolean;        // loop control variable
begin
  {setup the loop control for a continuous loop}
  Waiting := TRUE;

  {indicate that the application is waiting for the change notification to fire}
  Button1.Enabled := FALSE;
  StatusBar1.SimpleText := 'Now waiting for a filename change';
  Application.ProcessMessages;

  {enter the loop}
  while Waiting do
  begin
    {at this point, the application is suspended until the notification
     object is signaled that a filename change has occured in the
     selected directory (this includes file deletions)}
    dwResult := WaitForSingleObject(NotificationHandle,INFINITE);
    if (dwResult = WAIT_OBJECT_0) then
    begin
      {indicate that the notification object was signaled}
      ShowMessage('The selected directory signaled a filename change');

      {query the user to see if they wish to continue monitoring this
       directory}
      if Application.MessageBox('Do you wish to continue monitoring this directory?',
                 'Continue?', MB_ICONQUESTION or MB_YESNO) = IDYES then
        {if the user wishes to continue monitoring the directory, reset
         the notification object and continue the loop...}
        FindNextChangeNotification(NotificationHandle)
      else
        {...otherwise break out of the loop}
        Waiting := FALSE;
    end;
  end;

  {close the notification object}
  FindCloseChangeNotification(NotificationHandle);

  {reset UI elements}
  Button1.Enabled := FALSE;
  Button2.Enabled := TRUE;
  StatusBar1.SimpleText := '';
  FileListBox1.Update;
end;

end.


this will tell you when a file changes in the directory you select to moniter.
in delphi put the cursor after the text FILE_NOTIFY_ press ctrl/space hee to see some more things you can moniter.
FILE_NOTIFY_CHANGE_FILENAME
FILE_NOTIFY_CHANGE_DIRNAME ETC..
Regards Barry
0
 
intheCommented:
ps if its easier i'll email you the project files.
0
 
MadshiCommented:
For NT I recommend using ReadDirectoryChangesW instead of the FindFirstFileNotification stuff, because ReadDirectoryChangesW tells you WHAT has changed, FindFirstFileNotification does not...
But ReadDirectoryChangesW is only available under NT.

Regards, Madshi.
0
 
intheCommented:
and i should've read the question title better(its too small now)i missed the nt bit.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now