Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to get a list of subdirectories and files in a folder Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
18-Oct-02
Category
Files Operation
Language
Delphi 2.x
Views
78
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Jonas Bilinkevicius

I want to automatically search a directory and get a listing of all the 
subdirectories and files on that drive.

Answer:

Solve 1:

Here is one way:
1   
2   function FindFiles(Directory: string; InclAttr, ExclAttr: Integer;
3     const SubDirs: Boolean; const Files: TStrings): Integer;
4   var
5     SearchRec: TSearchRec;
6   begin
7     Directory := IncludeTrailingPathDelimiter(Directory);
8     FillChar(SearchRec, SizeOf(SearchRec), 0);
9     if FindFirst(Directory + '*.*', faAnyFile, SearchRec) = 0 then
10    begin
11      try
12        repeat
13          if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
14            if ((SearchRec.Attr and InclAttr > 0) or ((SearchRec.Attr = 0) and 
15  (InclAttr
16              <> faDirectory))) and
17              (SearchRec.Attr and ExclAttr = 0) then
18            begin
19              Files.Add(Directory + SearchRec.Name);
20              if SubDirs then
21                if SearchRec.Attr and faDirectory <> 0 then
22                  FindFiles(Directory + SearchRec.Name, InclAttr, ExclAttr, SubDirs,
23                    Files);
24            end;
25        until
26          FindNext(SearchRec) <> 0;
27      finally
28        FindClose(SearchRec);
29      end;
30    end;
31    Result := Files.Count;
32  end;
33  
34  //Example of usage:
35  
36  procedure TForm1.Button1Click(Sender: TObject);
37  var
38    sl: TStringList;
39  begin
40    Memo1.Clear;
41    sl := TStringList.Create;
42    try
43      FindFiles('c:\', faAnyFile, 0, True, sl);
44      Memo1.Lines.Add('Number of directories/files: ' + IntToStr(sl.Count));
45      Memo1.Lines.AddStrings(sl);
46    finally
47      sl.Free;
48    end;
49  end;



Solve 2:
50  
51  procedure GetFiles(APath: string; AExt: string; AList: TStrings; ARecurse: boolean);
52  var
53    theExt: string;
54    searchRec: SysUtils.TSearchRec;
55  begin
56    if APath[Length(APath)] <> '\' then
57      APath := APath + '\';
58    AList.AddObject(APath, Pointer(-1));
59    if FindFirst(APath + '*.*', faAnyFile, searchRec) = 0 then
60    try
61      repeat
62        with searchRec do
63        begin
64          if (Name <> '.') and (Name <> '..') then
65            if (Attr and faDirectory <= 0) then
66            begin
67              theExt := '*' + UpperCase(ExtractFileExt(searchRec.Name));
68              if (AExt = '*.*') or (theExt = UpperCase(AExt)) then
69                AList.AddObject(searchRec.Name, Pointer(0))
70            end
71            else
72            begin
73              if ARecurse then
74              begin
75                GetFiles(APath + Name + '\', AExt, AList, ARecurse);
76              end;
77            end;
78        end;
79        Application.ProcessMessages;
80      until
81        FindNext(searchRec) <> 0;
82    finally
83      SysUtils.FindClose(searchRec);
84    end;
85  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC