Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looking at a directory to check for changes

Status
Not open for further replies.

tjcusick

Programmer
Dec 26, 2006
134
US
I'm using Delphi 7.
I'm just not sure where to start with this one. A co-worker comes to me and says "I want to monitor a directory and when all the files get put in there I want to create a txt file with all the file names and directory path in it."

So I'm thinking first i need to monitor the directory with maybe a timer, like check it every 3-5 minutes, and would have to compare the file names and sizes, (in a tstring maybe) to see if they are still changing and when they are no longer changing then I could start creating the txt file with a listing of all the files in that directory (with directory path)...

Does that sound reasonable?

Next question is how would i go about monitoring the directory?

Any/All suggestions are welcome...

Thanks

Tom Cusick
 
You can register an eventhandler that gets called by the OS when a file changes in a/some specified location, I even found some util that can do specific tasks when that happens. Can't remember the name though (> 5 years ago) Google could help you I guess...

HTH
TonHu
 
i havent tried it just found it on the net.

Detect changings of a directory
Use FindFirstChangeNotification function for detecting of changing (creating/removing) directory.
Code:
procedure TForm1.Timer1Timer(Sender: TObject);
var
  Dir: string;
begin
  Dir:=Edit1.Text;
  if FindFirstChangeNotification(
       PChar(Dir),
       False,
       FILE_NOTIFY_CHANGE_DIR_NAME)<>INVALID_HANDLE_VALUE then
    Label1.Caption:=Dir+' directory presents'
  else
    Label1.Caption:=Dir+' directory absents';
end;


Return all files in directory
Use SearchFiles procedure. You should specify a directory, and after that, this function returns a list of files in this folder.
Main moments are FindFirst and FindNext functions.
Code:
procedure TForm1.SearchFiles(St: string);
var
  MySearch: TSearchRec;
  FindResult: Integer;
begin
  FindResult:=FindFirst(St+'\*.*', faAnyFile, MySearch);
  if (MySearch.Name<>'.')and(MySearch.Name<>'..') then
    Memo1.Lines.Add(MySearch.Name);
  while FindNext(MySearch)=0 do
  begin
    if (MySearch.Attr<>faDirectory)and
      (MySearch.Name<>'.')and
      (MySearch.Name<>'..') then
      Memo1.Lines.Add(MySearch.Name);
  end;
end;

Aaron
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top