Board index » delphi » Search multiple files in multiple directories

Search multiple files in multiple directories

Hi there,

I'm a poor lonesome 'beginner' programmer from Polytechnical school of
Lausanne (Yes a Swiss guy).

I'm just trying to get in a list all files with a certain extension out of
a directory. OK I use FindFirst and FindNext until this directory is
complete.

But then I would like to scan all subdirectories... Can someone help me on
this one?

 

Re:Search multiple files in multiple directories


  Hi Dada !

  I'm sitting just a few 100 meters from you and I have the solution...
Sorry if my answer is not in french but it could be usefull for another
guy in the world...

  What you need is to explore a tree, so the best way is to use a
recursive procedure to do it. I've written something like that but it's
at home, not here in my office. The principle is the following (in
pseudo-code) :

procedure ExploreTree(Dir:string);
begin
  while ThereAreNotExploredDirectoriesInCurrentFolder do  
    ExploreTree(FindNextDirectoryInTheCurrentFolder); // Yeah! It's
recursive
  ExploreFilesInCurrentFolderAndAddThemToYourList;
end;  

  Hope this help...

Quote
Dada wrote:

> Hi there,

> I'm a poor lonesome 'beginner' programmer from Polytechnical school of
> Lausanne (Yes a Swiss guy).

> I'm just trying to get in a list all files with a certain extension out of
> a directory. OK I use FindFirst and FindNext until this directory is
> complete.

> But then I would like to scan all subdirectories... Can someone help me on
> this one?

--
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
 Alexandre HIRZEL                                                  
 Labo. de Zoologie et d'Ecologie Animale
 Institut d'Ecologie                            Tel:(+4121) 692 41 76
 Faculte des Sciences, Bat. Biologie            Fax:(+4121) 692 41 05
 Universite de Lausanne                                            
 CH-1015 LAUSANNE              mailto:Alexandre.Hir...@ie-zea.unil.ch
 SWITZERLAND    http://www.unil.ch:8080/izea/pages/recherche/ibex.html  
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\

Re:Search multiple files in multiple directories


Try this procedure. It assumes there is a listbox to add the names to but
you can change that to anything you want. Also, the boolean variable
StopItNow is set to true by pressing a cancel button. This too can be
changed to whatever you want or taken out completely.

Woody

procedure myform.GetFiles(Path: string;FileMask: string);
label
 GetNextOne;
var
 Rec: TSearchRec;
begin
 if ListBox1.Items.Count > 1500 then
   exit;
 FindFirst(Path + '\*.*',faAnyFile,Rec);
GetNextOne:
  if (Rec.Name <> '.') and (Rec.Name <> '..') then
  begin
   if (Rec.Attr and faDirectory) <> 0 then
     GetFiles(Path + '\' + Rec.Name,FileMask)
    else if (FileMask = '*.*') or
        (LowerCase(ExtractFileExt(Rec.Name)) =
lowercase(ExtractFileExt(FileMask))) then
     ListBox1.Items.Add(Rec.Name);
  end;
  Application.ProcessMessages;
  if StopItNow then
   exit;
  if FindNext(Rec) <> 0 then
    exit;
  goto GetNextOne;
end;

Other Threads