Directory watch class, that can monitor the selected directory included with sub-directories. Main features:

  • Simple to use class, that enables you to watch for all changes in a given directory
  • Watch only the selected directory or also all its sub-directories
  • You can define exactly what notifications you will receive
DirectoryWatch download: Version 1.3.0 (21.07.2012)

The demo bellow shows the basic usage of DirectoryWatch. If you have trouble using the code or have any other questions, please contact me directly using the “Contact” page.

Sample usage:

procedure TfMain.btnStartClick(Sender: TObject);
begin
  FDirectoryWatch.WatchSubTree := cbWatchSubdirectories.Checked;
  FDirectoryWatch.Directory := eDirectoryName.Text;
  FDirectoryWatch.Start;
 
  btnStart.Enabled := False;
  btnStop.Enabled := True;
end;
procedure TfMain.OnNotify(const Sender: TObject;
                          const Action: TWatchAction;
                          const FileName: string);
var
  ListItem: TListItem;
begin
  lvWatchActions.Items.BeginUpdate;
  try
    ListItem := lvWatchActions.Items.Add;
    ListItem.Caption := GetEnumName(TypeInfo(TWatchAction), Integer(Action)) ;
    ListItem.SubItems.Add(FileName);
  finally
    lvWatchActions.Items.EndUpdate;
  end;
end;

Change Log

  • 1.3.0
    • Added buffer size property, so that the user can control the size of buffer
    • Added OnError event handler, so user get notified of all errors
    • Check the result of ReadDirectoryChangesW with GetOverlappedResult. This way we can check if buffer overflow happens and trigger an error.
  • 1.2.1
    • Close the FChangeEvent handle
  • 1.2.0
    • WaitForFileReady added. Waits until file is free to be used
  • 1.1.0
    • 64 bit compiler compatible
    • Fixed thread termination bug
  • 1.0.1
    • Refactored the main watch loop
  • 1.0.0
    • Initial code rewrite from “FnugryDirWatch”