2009. november 14., szombat

Monitor a directory and take action when files are added


Problem/Question/Abstract:

I need to monitor a series of directories, and perform a selective deletion (based on file date) when they reach (or go over) a certain size.

Answer:

The snippet below is a procedure I wrote to monitor a directory and take action when files are added to that directory. It uses these WinAPI functions to accomplish that purpose:

FindFirstChangeNotification
WaitForSingleObject
FindNextChangeNotification
FindCloseChangeNotification

If you look these up in the help, you may be able to solve your problem using a similar technique.

procedure TDosNotifyThread.Execute;
begin
  FChangeHandle := FindFirstChangeNotification(pchar(cRequestDir), false,
    FILE_NOTIFY_CHANGE_FILE_NAME);
  repeat
    FExitWait := WaitForSingleObject(FChangeHandle, cThreadCycleTime); {See GLOBALS}
    if FExitWait = WAIT_OBJECT_0 then
      PostMessage(MilerForm.Handle, DO_REQUEST, 0, 0);
    FindNextChangeNotification(FChangeHandle);
  until
    RTQ or Terminated;
  FindCloseChangeNotification(FChangeHandle);
  if not Terminated then
    Terminate;
end;

Nincsenek megjegyzések:

Megjegyzés küldése