Recent

Author Topic: How to use TFileSearcher to count files in a directory  (Read 11805 times)

Gizmo

  • Hero Member
  • *****
  • Posts: 831
How to use TFileSearcher to count files in a directory
« on: May 28, 2011, 12:21:22 am »
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 :

Code: Pascal  [Select][+][-]
  1. if DirPathExists(ChosenDirectory) then
  2.   begin                              
  3.     FS := TFileSearcher.Create;
  4.     FS.OnFileFound := @SomeProcedure;    // Do a load of stuff with each file whenever the next line find a file  
  5.     FS.Search(ChosenDirectory, '*.*', True);  
  6.     FS.Free;    
  7.   end
  8.  

However, I want to do something like this to count the files first:

Code: Pascal  [Select][+][-]
  1. if DirPathExists(ChosenDirectory) then
  2.   begin
  3.     FS1 := TFileSearcher.Create;    
  4.     //Count all the files in ChosenDirectory and assign to NoOfFiles
  5.     Label1.Caption := 'There are ' + IntToStr(NoOfFiles) + ' files in the directory');
  6.                              
  7.       FS2 := TFileSearcher.Create;    
  8.       FS2.OnFileFound := @SomeProcedure;      
  9.       FS2.Search(ChosenDirectory, '*.*', True);  
  10.      
  11.     FS1.Free;    
  12.     FS2.Free;    
  13.   end;
  14.  

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 :

Code: Pascal  [Select][+][-]
  1. if DirPathExists(ChosenDirectory) then
  2.   begin
  3.     FS1 := TFileSearcher.Create;    
  4.     FS1.OnFileFound := @FileCounter    // For every file that is found on the next line, inc count
  5.     FS1.Search(ChosenDirectory, '*.*', True);  
  6.     Label1.Caption := 'There are ' + IntToStr(NoOfFiles) + ' files in the directory');
  7.      
  8. // Now, having counted all the files, execute the SomeProcedure                      
  9.       FS2 := TFileSearcher.Create;    
  10.       FS2.OnFileFound := @SomeProcedure;      
  11.       FS2.Search(ChosenDirectory, '*.*', True);  
  12.    
  13.     FS1.Free;    
  14.     FS2.Free;
  15.   end;
  16.  
« Last Edit: May 28, 2011, 12:32:18 am by tedsmith »

Dibo

  • Hero Member
  • *****
  • Posts: 1048
Re: How to use TFileSearcher to count files in a directory
« Reply #1 on: May 28, 2011, 12:39:18 am »
Maybe you can use some functions from FileUtil unit like "FindAllFiles"? You can easily set search path and file mask, example to use:
Code: Pascal  [Select][+][-]
  1. uses FileUtil
  2.  
  3. procedure TForm1.Button1Click(Sender: TObject);
  4. var
  5.   sl: TStringList;
  6.   i: Integer;
  7. begin
  8.   sl := FindAllFiles('C:\Lazarus\Projects\search_test_dir', '*.pas', True);
  9.   try
  10.     Memo1.Append('There are ' + IntToStr(sl.Count) + ' pascal files in directory');
  11.     for i:=0 to sl.Count-1 do
  12.       Memo1.Append('File'+IntToStr(i)+': ' + sl.Strings[i]);
  13.   finally
  14.     sl.Free;
  15.   end;
  16. end;
  17.  

eny

  • Hero Member
  • *****
  • Posts: 1634
Re: How to use TFileSearcher to count files in a directory
« Reply #2 on: May 28, 2011, 12:52:27 am »
What you need is a 'wrapper class' around TFileSearcher that stores all files found internally in a list.
That class already exists as TListFileSearcher (FindAllFiles uses it) but silly enough it's private to FileUtil so it cannot be re-used. Which is clearly a bug.
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Re: How to use TFileSearcher to count files in a directory
« Reply #3 on: May 28, 2011, 01:35:00 am »
Thanks both of you.

I had used FindAllFiles before but forgot about it! I have implemented it into my code and it works a treat, and very quickly. I am now using the "Initial File Count" followed by the "Processed so far file count" to try and generate a percentage of progress. Odly, my percentages are not working but that's another subject!

Thanks again for the help - it is very useful

 

TinyPortal © 2005-2018