• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 183
  • Last Modified:

Creating a list of all the sub directories in a directory

I am using Delphi 2006 VCL.  I need code to find the names of all the sub-driectories in a directory.  Can someone help?
0
mcmahling
Asked:
mcmahling
1 Solution
 
mikelittlewoodCommented:
Drop an edit box and button onto the form.
Put the directory name you want to search in the edit box, i.e. 'C:\'

function SearchDir(sDir: string): string;
var
 sr: TSearchRec;
 i: integer;
 sl: TStringList;
begin
  try
    sl := TStringList.Create;
    sl.Sorted := True;
    i := findFirst(sDir+'*.*',faAnyFile,sr);
    while i = 0 do
    begin
      if (sr.name <> '.') and (sr.name <> '..') then
      begin
        if (sr.attr = faDirectory)then
          sl.Add(sDir+sr.name);
      end;
      i := findNext(sr);
    end;
    FindClose(sr);
    Result := sl.Text;
  finally
    FreeAndNil(sl);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  s: string;
begin
  s := SearchDir(Trim(Edit1.Text));
  ShowMessage(s);
end;

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now