Solved

New file notification

Posted on 2001-06-20
4
327 Views
Last Modified: 2010-04-06
I need to process files on at a time as they are dropped into a folder by some other part of a system.

Using a timer and checking the contents of the folder is the obvious solution, but is there any way that I can be notified by the OS when a new file is created in the folder and respond to the event?

Regards,
Ben
0
Comment
Question by:benjvr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 

Expert Comment

by:d32coder
ID: 6210333
Try these components on Torry's.

http://www.torry.net/notification.htm

Don
0
 
LVL 1

Expert Comment

by:Greyman
ID: 6212596
Have a look in the Win API help file - read about the function 'FindFirstChangeNotification' - this function tells the OS to keep you informed about changes to a directory.
0
 
LVL 17

Expert Comment

by:inthe
ID: 6212702
an example from usenet:

unit FindNextChange;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TFrmFindNextChange = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    Label2: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FrmFindNextChange: TFrmFindNextChange;

function FindFirstChangeNotificationA(lpPathName: PAnsiChar;
  bWatchSubtree: BOOL; dwNotifyFilter: DWORD): THandle; stdcall;

implementation

{$R *.DFM}

function FindFirstChangeNotificationA; external kernel32 name
'FindFirstChangeNotificationA';

function WinErrorAsString(AErrCode: DWORD): string;
var
  lng: integer;
begin
  SetLength(Result, MAX_PATH + 1);
  lng := FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
                       nil,
                       AErrCode,
                       LANG_SYSTEM_DEFAULT,
                       PChar(Result),
                       MAX_PATH,
                       nil);

  if lng > 0 then
    SetLength(Result, lng)
  else
    Result := 'Unknown error';
end;

procedure TFrmFindNextChange.Button1Click(Sender: TObject);
const
  count: integer = 0;
var
  done:           boolean;
  dwWaitStatus:   DWORD;
  dwChangeHandle: THandle;
begin
  Button1.Enabled := false;
  try
    dwChangeHandle := FindFirstChangeNotificationA(
//                      'I:\Dev\Delphi\32\QandD\',
                      'C:\Windows',
                      BOOL(1),
                      FILE_NOTIFY_CHANGE_FILE_NAME
                      or FILE_NOTIFY_CHANGE_ATTRIBUTES
                      or FILE_NOTIFY_CHANGE_SIZE
                      or FILE_NOTIFY_CHANGE_LAST_WRITE
                      );

    if dwChangeHandle = INVALID_HANDLE_VALUE then begin
      Label1.Caption := 'Error: ' + WinErrorAsString(GetLastError);
      Exit;
    end;

    done := false;
    repeat
      dwWaitStatus := WaitForSingleObject(dwChangeHandle, 30000);

      case dwWaitStatus of
        WAIT_OBJECT_0: begin
          Inc(count);
          Label2.Caption := IntToStr(count);
          Label2.Update;
          Application.ProcessMessages;

          if FindNextChangeNotification(dwChangeHandle) = false then
            Exit;
        end;

        WAIT_ABANDONED,
        WAIT_TIMEOUT,
        WAIT_FAILED:  done := true;
      end;
    until done;
  finally
    Button1.Enabled := true;
  end;
end;

end.
0
 

Accepted Solution

by:
gi7mhl earned 100 total points
ID: 6213666
You can download a component that monitors events occurring to a folder(deletions, additions). The site for the component is http://www.rtfm.be/fpiette/indexuk.htm and the component is called DirMon. With the component you specify the folder that you want it to monitor and respond to events using its OnChange event. Or you can check this site for directory monitoring events http://www.torry.net/notification.htm
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question