Solved

Filling a listbox with the names of sub-directories

Posted on 2006-11-03
4
158 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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
error 1.1 400 Bad request idhttp delphi 18 87
Delphi 2 59
Moving (cutting/pasting) controls in a TTabbedNotebook... 7 34
Tidtcpserver listening on multiports? 1 27
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

770 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