Solved

Filling a listbox with the names of sub-directories

Posted on 2006-11-03
4
160 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…

679 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