I.T. Discussion Community!
-Collapse +Expand
Delphi
Search Delphi Group:

Advanced
-Collapse +Expand Delphi To/From
To/FromCODEGuides
-Collapse +Expand Delphi Store
PRESTWOODSTORE

Prestwood eMagazine

July Edition
Subscribe now! It's Free!
Enter your email:

   ► MB LobbyPascal and Delphi Coding BoardDelphi Object PASCAL Topic   Print This     

How to get the subdirectories?

How to get the subdirectories? in Delphi Object PASCAL topic (part of our Pascal and Delphi Coding group).

Quick Search: subdirectories   How get   How get subdirectories  
WhiteWolve
 (Inactive)
-- USA
I have been searching a lot for a way to get the subdirectories of any directory. I haven't found a way yet so I hope you can help me.
 Posted 18 years ago (Thread Starter)
Comment Quote
About WhiteWolve -Collapse +Expand
Visit Profile
Inactive member.
Member does not subscribe to this thread.
Email Not Verified!
Once email is verified, we will review and approve the account.
Web Presence Hidden.
Once above is taken care of, full Profile content will display including back links, about me, my message, custom Profile html, social networking links, message board signature, company profile, etc.

Post ID #4684, 4 replies
Thread Started 12/28/2001 8:05:00 AM
View Counter=3699
Last Reply Posted 6/3/2002 3:49:00 PM)
Location=-- USA 
Joined=18 years ago   MB Posts=1  
Larry J. Rutledge
Prestwood IT
Citrus Heights, CA USA
If all you want is to obtain a list of subdirectories within a specified path, you can use FindFirst/FindNext as in the following code:

procedure Form1.GetDirList(sPath: string);
var
Found: Integer;
SearchRec: TSearchRec;
begin
Found := SysUtils.FindFirst(IncludeTrailingBackslash(sPath)
+ '*.*', faDirectory, SearchRec);
try
while Found = 0 do
begin
if ((SearchRec.Attr and faDirectory) = faDirectory) and (SearchRec.Name[1] <> '.') then
ListBox1.Items.Add(SearchRec.Name);
Found := SysUtils.FindNext(SearchRec);
end;
finally
SysUtils.FindClose(SearchRec);
end;
end;
However, if you want to get the subdirectories within a specified path AND the subdirectories under those, then you need to use a recursive procedure. A recursive procedure (or function) is one that calls itself. (NOTE: The most important thing to remember when creating recursive methods is to make sure there is some condition to exit from the method at some point!) Here is a procedure that will obtain all subdirectories from a specified path down:

procedure Form1.GetSubDirList(sPath: string);
var
Found: Integer;
SearchRec: TSearchRec;
begin
Found := SysUtils.FindFirst(IncludeTrailingBackslash(sPath) + '*.*', faDirectory, SearchRec);
try
while Found = 0 do
begin
if ((SearchRec.Attr and faDirectory) = faDirectory) and (SearchRec.Name[1] <> '.') then
begin
ListBox1.Items.Add(IncludeTrailingBackslash(sPath) + SearchRec.Name);
Application.ProcessMessages;
// Recursive method call
GetSubDirList(IncludeTrailingBackslash(sPath) + SearchRec.Name);
end;
Found := SysUtils.FindNext(SearchRec);
end;
finally
SysUtils.FindClose(SearchRec);
end;
end;
Hope this helps,
Larry
Larry J. Rutledge

Analyst

Prestwood Software

www.prestwood.com
 Posted 17 years ago
Comment Quote
About Larry J. Rutledge -Collapse +Expand
Visit Profile
Approved member.
Member subscribes to this thread with a verified email.
About Larry J. Rutledge
I worked for Prestwood Software as a Delphi developer from 1997 through 2002. During that time I enjoyed working with Mike Prestwood and the other developers at Prestwood.

Post ID #4853 (Level 1.1)  Reply to 4684
Thread Started 1/15/2002 9:58:00 AM
View Counter=2
Location=Citrus Heights, CA USA 
Joined=19 years ago   MB Posts=73   KB Posts=9  
Rick Carter
Cincinnati OH USA
A while back there was an article, some forum discussion, and a free component for extending the functionality of FindFirst and FindNext at
Old UBB Archived Link: [URL=http://delphi.about.com/]http://delphi.about.com/[/URL]
Rick Carter
Chair, Delphi/Paradox SIG
Cincinnati PC Users Group
 Posted 17 years ago
Comment Quote
About Rick Carter -Collapse +Expand
Visit Profile
Approved member.
Member subscribes to this thread with a verified email.
Old Account!
If this is your account, sign in to activate web presence data (sign in quarterly to keep active). Alternatively, you can subscribe to our monthly eMag with a valid email address.
Web Presence Hidden.
Once above is taken care of, full Profile content will display including back links, about me, my message, custom Profile html, social networking links, message board signature, company profile, etc.

Post ID #4713 (Level 1.2)  Reply to 4684
Reply Posted 1/2/2002 5:09:00 AM
Location=Cincinnati OH USA  
Joined=18 years ago   MB Posts=518  
bbegley
Lincoln, CA
We use code that is very similiar to this, but we don't call Application.ProcessMessages. What is the benefit of making that call here? Is it advisable to do this before making a recursive call, and if so, why?
 Posted 17 years ago
Comment Quote
About bbegley -Collapse +Expand
Visit Profile
Membership pending.
Member subscribes to this thread but email is NOT VERIFIED.
Email Not Verified!
Once email is verified, we will review and approve the account.
Web Presence Hidden.
Once above is taken care of, full Profile content will display including back links, about me, my message, custom Profile html, social networking links, message board signature, company profile, etc.

Post ID #6440 (Level 1.3)  Reply to 4684
Reply Posted 6/3/2002 8:35:00 AM
Location=Lincoln, CA  
Joined=17 years ago   MB Posts=2  
Most Recent Post
Scott Wehrly
Prestwood IT
 (Inactive)
Las Vegas, NV USA
The recursive call to GetSubDirList doesn't trigger the Windows Event Handler until it returns from the last call. So "Application.ProcessMessages" triggers the event handler to repaint the lines in the list box as they are being written.
 Posted 17 years ago
Comment Quote
About Scott Wehrly -Collapse +Expand
Visit Profile
Inactive member.
Member does not subscribe to this thread.
Old Account!
If this is your account, sign in to activate web presence data (sign in quarterly to keep active). Alternatively, you can subscribe to our monthly eMag with a valid email address.
Web Presence Hidden.
Once above is taken care of, full Profile content will display including back links, about me, my message, custom Profile html, social networking links, message board signature, company profile, etc.

Post ID #6468 (Level 1.4)  Reply to 4684
Reply Posted 6/3/2002 3:49:00 PM
Location=Las Vegas, NV USA 
Joined=17 years ago   MB Posts=442   KB Posts=19  

Revive Thread!

Add a comment to revive this old thread and make this archived thread more useful.

Write a Comment...
Full Editor
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1174A1
Enter key:
Icon: A Post    Thread    Idea    Important!    Cool    Sad    No    Yes    Includes a Link...   
Thread #4684 Counter
3699
Since 4/2/2008
Follow PrestwoodBoards on: 


©1995-2019 PrestwoodBoards  [Security & Privacy]
Professional IT Services: Coding | Websites | Computer Tech