Watching Directories

delphi package - easy access to shell apis
Post Reply
Muesli
Posts: 3
Joined: Mon Dec 12, 2005 6:04 am

Watching Directories

Post by Muesli »

Hello,

I am writing a program in Delphi 7, that does jobs, which are defined in template files being located in the applications directory.
Not I wanted to be informed when a new template file in this directory is created. So I found your madShell and it works fine. Thanks for this. :-)


But later, I noticed, that my program will also be notified, when files are created in sub directories although I did not indicated that.

Here is some code:

Code: Select all

  // I have made a new class which handles your madShell

Type
  TChangeType = (ctAll, ctFileCreated, ..., ctDirectoryRenamed);
  TChangeTypes = Set Of TChangeType;

  TDirectoryWatch = Class (TObject)
  Private
    fDirectory: String;
    fSubTree : Boolean;
   ...
    fOnFileCreated  : TFileEvent;
   ...

    procedure ShellEvent(event: TShellEventType; const obj1, obj2: IShellObj; drive: char; value: cardinal);
  Protected
    Procedure Initialize;
    Procedure Finalize;
   ...

    Procedure NotifyFileCreated (FileName: TFileName);
  Public
    Constructor Create (aPath: String; aSubTree: Boolean; aChangeTypes: TChangeTypes = [ctAll]); Overload;

   ...

    Property ChangeTypes: TChangeTypes Read fChangeTypes Write fChangeTypes;

    Property OnFileCreated: TFileEvent Read fOnFileCreated Write fOnFileCreated;
  End;

implementation

Constructor TDirectoryWatch.Create(aPath: String; aSubTree: Boolean; aChangeTypes: TChangeTypes);
Begin
  Inherited Create ();
  fDirectory := IncludeTrailingPathDelimiter (aPath);
  fChangeTypes := aChangeTypes;
  fSubTree := aSubTree;
  Initialize;
End;

procedure TDirectoryWatch.Finalize;
begin
  UnregisterShellEvent (ShellEvent);
end;

procedure TDirectoryWatch.Initialize;
begin
  RegisterShellEvent (ShellEvent, fDirectory, fSubTree);
end;

procedure TDirectoryWatch.NotifyFileCreated(FileName: TFileName);
begin
  If ((ctFileCreated In ChangeTypes) Or (ctAll In ChangeTypes)) Then
    If Assigned (fOnFileCreated) Then  
      fOnFileCreated (Self, FileName);
end;

procedure TDirectoryWatch.ShellEvent(event: TShellEventType; const obj1,
  obj2: IShellObj; drive: char; value: cardinal);
begin
  Case event Of
    seItemCreated: NotifyFileCreated (obj1.Path);
   ...
  End;
end;


In my program i create an instance of this class as follows:

Code: Select all

  // Path is the applications directory:
  // Path := ExtractFilePath (Application.ExeName);
  fDirectoryWatch:= TDirectoryWatch.Create (Path, False);
So the method to register the ShellEvent is called like this:

Code: Select all

  // fDirectory = 'D:\Sandbox\NavImport\Bin\'
  // fSUbTree = False
  RegisterShellEvent (ShellEvent, fDirectory, fSubTree);

Now being notified I can check whether the filepath is the same as the directories path, but I just want to ask, whether I made a mistake, or whether there is a bug in your madShell.

Greetings,
Muesli
madshi
Site Admin
Posts: 10754
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

Which OS are we talking about?
Muesli
Posts: 3
Joined: Mon Dec 12, 2005 6:04 am

Post by Muesli »

the program runs on windows 2000...
madshi
Site Admin
Posts: 10754
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

Sorry for the long delay. I've fixed this now. A new build will be availably soon.
Post Reply