Delphi32.com - Home!
| Home/News | Downloads | Forums | D32 Magazine | Resources | Info and Facts |  
 
 How do I recurse sub directories?


How do I recurse sub directories?   

    You will need to use Delphi's FindFirst() and FindNext() procedures to create a list of all the sub directories in a directory. For each sub directory, you will need to use Delphi's FindFirst() and FindNext() procedures, again to add any sub directories of the found directories to the list. Continue working down the list until all the directories have been processed. Example:

    procedure GetDirectories(const DirStr : string;
                             ListBox : TListBox);
    var
      DirInfo: TSearchRec;
      r : Integer;
    begin
      r := FindFirst(DirStr + '\*.*', FaDirectory, DirInfo);
      while r = 0 do  begin
        Application.ProcessMessages;
        if ((DirInfo.Attr and FaDirectory = FaDirectory) and
             (DirInfo.Name <> '.') and
             (DirInfo.Name <> '..'))  then
          ListBox.Items.Add(DirStr + '\' + DirInfo.Name);
        r := FindNext(DirInfo);
      end;
      SysUtils.FindClose(DirInfo);
    end;
    procedure GetFiles(const DirStr : string;
                       ListBox : TListBox);
    var
      DirInfo: TSearchRec;
      r : Integer;
    begin
      r := FindFirst(DirStr + '\*.*', FaAnyfile, DirInfo);
      while r = 0 do  begin
        Application.ProcessMessages;
        if ((DirInfo.Attr and FaDirectory <> FaDirectory) and
            (DirInfo.Attr and FaVolumeId <> FaVolumeID)) then
          ListBox.Items.Add(DirStr + '\' + DirInfo.Name);
        r := FindNext(DirInfo);
      end;
      SysUtils.FindClose(DirInfo);
    end;
    procedure TForm .FormCreate(Sender: TObject);
    var
      i : integer;
    begin
      ListBox .Items.Clear;
      ListBox2.Items.Clear;
      ListBox .Items.Add('C:\Delphi');
      GetDirectories('C:\Delphi', ListBox );
      i :=  ;
      while i < ListBox .Items.Count do begin
        GetDirectories(ListBox .Items[i], ListBox );
        Inc(i);
      end;
    end;
    procedure TForm .ListBox Click(Sender: TObject);
    begin
      ListBox2.Clear;
      GetFiles(ListBox .Items[ListBox .ItemIndex],
               ListBox2);
    end;
    Note: Recursing directories can take up a lot of memory. It is suggested that you consider creating a linked list or a temporary file to store your list entries instead of using a component (such as a Memo or StringList), as there are limits to the number of entries these components can have. 


  << Previous Faq     Complete List     Next Faq >>  



 
 Hits/month  2,500,000+ 
 Downloads
 (Since May 2000)
 7,393,709 
 Total Files  6,023 
 Forum msgs  7,670 
 Articles/FAQs  70+/900+ 
Kylix
Tips n Tricks
FAQs
Knowledge Base
Bug Listings
Articles
Books
Newsgroups
Links
Submissions
Testimonials
Advertising
Contact Us
About Us
Search Amazon:
Top Selling Software at Amazon

| Home/News | Downloads | Forums | Resources | Info and Facts | Testimonials |
  Site Search:
 


Comments/Problems: Webmaster@delphi32.com
Copyright © 1998-2006, Delphi32.com. All rights reserved.
Terms of Use