Board index » delphi » find next for each subdirectory not working

find next for each subdirectory not working

i've been trying to write a program to search through each file in a
directory and each file in all it's subdirectories.  i've come close
by doing something like -

getdir(0,path);
search(start_path);
path := start_path + '\';

procedure search(path : string);
find first(path+*.*,...)
if find next(searching) <> 0
        and
        (searching.fadirectory) and (searching.name <> '.') and
        (searching.name <> '..')
        then begin
        path := searching.name;
        chdir(start_path+path);
        search(path);
        end;    

i've had alot of programmers say how easy it is and i've asked a few of
them to write the code and prove it. none have yet.  some have given my
half completed examples of the basic idea which i already know.  can
anyone show me an exact sample or tell me where i can find one?

--
Please reply by Post & E-Mail or just E-Mail.

 

Re:find next for each subdirectory not working


Quote
In article <3288963F....@concentric.net> laserjet <laser...@concentric.net> writes:
>i've been trying to write a program to search through each file in a
>directory and each file in all it's subdirectories.  i've come close
>by doing something like -
>getdir(0,path);
>search(start_path);
>path := start_path + '\';
>procedure search(path : string);
>find first(path+*.*,...)
>if find next(searching) <> 0
>        and
>        (searching.fadirectory) and (searching.name <> '.') and
>        (searching.name <> '..')
>        then begin
>        path := searching.name;
>        chdir(start_path+path);
>        search(path);
>        end;    
>i've had alot of programmers say how easy it is and i've asked a few of
>them to write the code and prove it. none have yet.  some have given my
>half completed examples of the basic idea which i already know.  can
>anyone show me an exact sample or tell me where i can find one?

Basically, the problem is that there is a definite (and small!) limit on the
number of Finds that you can have going at one time.  It's such that a
recursive solution like this one generally does not work well.

What does work well is an approach that uses a stringlist for a stack.  You
push the starting directory on the stack.  Then you loop:  pop a directory,
search it, pushing all directory-names found back on the stack (push complete
paths).  When you're done, do a FindClose() on the search-block, pop a name
off the stack, and repeat until the stack is empty.  Remember, as you do, to
ignore the dirnames "." and "..".

This non-recursive solution uses only one search block throughout the entire
process and it seems to work well.  I must, alas, leave the coding as an
exercise to the reader.

/mr/

Re:find next for each subdirectory not working


On 12 Nov 1996 10:18:01 -0700, i...@sundialservices.com (Sundial

Quote
Services) wrote:
>In article <3288963F....@concentric.net> laserjet <laser...@concentric.net> writes:

>>i've been trying to write a program to search through each file in a
>>directory and each file in all it's subdirectories.  i've come close
>>by doing something like -

>>getdir(0,path);
>>search(start_path);
>>path := start_path + '\';

>>procedure search(path : string);
>>find first(path+*.*,...)
>>if find next(searching) <> 0
>>        and
>>        (searching.fadirectory) and (searching.name <> '.') and
>>        (searching.name <> '..')
>>        then begin
>>        path := searching.name;
>>        chdir(start_path+path);
>>        search(path);
>>        end;    

>>i've had alot of programmers say how easy it is and i've asked a few of
>>them to write the code and prove it. none have yet.  some have given my
>>half completed examples of the basic idea which i already know.  can
>>anyone show me an exact sample or tell me where i can find one?

{this works with D1 and D2}

{this procedure assumes that Start has an ending \}
procedure SearchAllDirs (Start : String);
var
  DirList : TStringList;

  procedure SearchOneDir;
  var
    SR : TSearchRec;
    DosRes : Integer;
    ThisDir : String;
  begin
    ThisDir := DirList [0];
    DirList.Delete (0);
    DosRes := FindFirst (ThisDir + '*.*', faAnyFile, SR);
    while DosRes = 0 do begin
      if (SR.Attr and faDirectory) <> 0 then
        if (SR.Name <> '.') and (SR.Name <> '..') then begin
          DirList.Add (ThisDir + SR.Name + '\');
        end;
      else if (SR.Attr and faVolumeID) = 0 then begin
        {do something with ThisDir + SR.Name}
      end;
      DosRes := FindNext (SR);
    end;
    FindClose (SR);
  end;

begin
  DirList := TStringList.Create;
  DirList.Add (Start);
  while DirList.Count > 0 do
    SearchOneDir;
  DirList.Free;
end;

Hope this helps
  Meikel Weber
make sure you visit my homepage
http://ourworld.compuserve.com/Homepages/meikel/mew.htm

Other Threads