I have a program that already utlises TFileSearcher. It will happily search any given directory, 'do stuff' with each file and then exit.
However, I want it to count the files in the given directory before it goes of to do stuff with the files in it.
I realise I can use FindFirst and FindNext, but seeing as I'm using TFileSearcher already, I think it makes sense to stick with that and TFileSearcher has the OnFileFound flag which I am sure must be able to be used to flick a counter up by one.
However, I am struggling to work out how to 'easily' use it to count the files, without creating a seperate @CountFiles procedure. At the moment, my TFileSearcher code that does the rest of the stuff in my program looks like this :
if DirPathExists(ChosenDirectory) then
begin
FS := TFileSearcher.Create;
FS.OnFileFound := @SomeProcedure; // Do a load of stuff with each file whenever the next line find a file
FS.Search(ChosenDirectory, '*.*', True);
FS.Free;
end
However, I want to do something like this to count the files first:
if DirPathExists(ChosenDirectory) then
begin
FS1 := TFileSearcher.Create;
//Count all the files in ChosenDirectory and assign to NoOfFiles
Label1.Caption := 'There are ' + IntToStr(NoOfFiles) + ' files in the directory');
FS2 := TFileSearcher.Create;
FS2.OnFileFound := @SomeProcedure;
FS2.Search(ChosenDirectory, '*.*', True);
FS1.Free;
FS2.Free;
end;
Try as I might, I can't work out where in TFileSearcher there is any kind of counting mechanism.
Is it just a case that I will have to do something like this :
if DirPathExists(ChosenDirectory) then
begin
FS1 := TFileSearcher.Create;
FS1.OnFileFound := @FileCounter // For every file that is found on the next line, inc count
FS1.Search(ChosenDirectory, '*.*', True);
Label1.Caption := 'There are ' + IntToStr(NoOfFiles) + ' files in the directory');
// Now, having counted all the files, execute the SomeProcedure
FS2 := TFileSearcher.Create;
FS2.OnFileFound := @SomeProcedure;
FS2.Search(ChosenDirectory, '*.*', True);
FS1.Free;
FS2.Free;
end;