Snackmoore
Snackmoore

Reputation: 935

Tracking changes made to a folder in Delphi

I need to writing a Delphi program which will monitor a folder for changes (add, update, rename and removal of files).

I have seen suggestions to use theTShellChangeNotifier. Is this the correct solution for this problem? How should I use it?

Upvotes: 8

Views: 5557

Answers (3)

glob
glob

Reputation: 2960

i suggest using madShell

RegisterShellEvent(ShellEvent, pathToMonitor, false, [seItemCreated, seItemRenamed]);

//

procedure Tform.ShellEvent(event: TShellEventType; const obj1, obj2: IShellObj; drive: char; value: cardinal);
var
  filename: string;
  isReady: boolean;
begin
  if (event = seItemCreated) then
    filename := obj1.Path
  else if (event = seItemRenamed) then
    filename := obj2.Path
  else
    exit;

  // try to open to ensure it's read for reading
  repeat
    try
      TfileStream.Create(filename, fmOpenRead + fmShareExclusive).Free;
      isReady := true;
    except
      isReady := false;
      sleep(250);
    end;
  until (isReady) or (not FileExists(filename));

  OutputDebugString(pChar('ShellEvent: ' + filename));

end;

Upvotes: 0

jpfollenius
jpfollenius

Reputation: 16620

This question might help. mghie's answer shows how to properly use ReadDirectoryChangesW.

Upvotes: 5

Jorge Córdoba
Jorge Córdoba

Reputation: 52123

I think this article will help you: Monitoring System Shell Changes using Delphi

Basically it analyzes the TShellChangeNotifier, discards it and then goes for a TSHChangeNotify which is basically a wrapper for the SHChangeNotify windows api function.

Upvotes: 5

Related Questions