Solved

Filling a listbox with the names of sub-directories

Posted on 2006-11-03
4
159 Views
Last Modified: 2010-04-05
I am using Delphi 2006 bds.  I need some code to fill a list box with the paths of the sub-directories given a specified directory.  
0
Comment
Question by:mcmahling
  • 2
4 Comments
 
LVL 28

Accepted Solution

by:
TName earned 250 total points
ID: 17869073
>I am using Delphi 2006 bds.

.Net or Win32?
If Win32, you might give this a try, it works with D7:


procedure TForm1.ListDirs(const Directory: String; List:TStrings);
var
  SR: TSearchRec;
begin
  if FindFirst(Directory+'*' , faDirectory, SR) = 0 then try
    repeat
      if SR.Attr=faDirectory then
        List.Add(Directory + SR.Name)
    until FindNext(SR) <> 0;
  finally
    FindClose(SR);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
   ListDirs('C:\',ListBox1.Items);
end;
0
 
LVL 28

Expert Comment

by:TName
ID: 17869263
Or use IncludeTrailingPathDelimiter if you want to also pass dir paths without trailing backslash:

procedure TForm1.ListDirs(Directory: String; List:TStrings);
var
  SR: TSearchRec;
begin
  Directory:=IncludeTrailingPathDelimiter(Directory);
  if FindFirst(Directory+'*' , faDirectory, SR) = 0 then try
    repeat
      if SR.Attr=faDirectory then
        List.Add(Directory + SR.Name)
    until FindNext(SR) <> 0;
  finally
    FindClose(SR);
  end;
end;
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question