2007. október 24., szerda

How to get a list of all subdirectories in the current folder


Problem/Question/Abstract:

How to get a list of all subdirectories in the current folder

Answer:

Enumerating all folders in a subfolder/ directory:

procedure EnumFolders(root: string; folders: TStrings);

  procedure Enum(dir: string);
  var
    SR: TSearchRec;
    ret: Integer;
  begin
    if dir[length(dir)] <> '\' then
      dir := dir + '\';
    ret := FindFirst(dir + '*.*', faDirectory, SR);
    if ret = 0 then
    try
      repeat
        if ((SR.Attr and faDirectory) < > 0) and (SR.Name <> '.') and
                                                 (SR.Name <> '..') then
        begin
          folders.add(dir + SR.Name);
          Enum(dir + SR.Name);
        end;
        ret := FindNext(SR);
      until
        ret <> 0;
    finally
      FindClose(SR)
    end;
  end;

begin
  Folders.Clear;
  if root < > emptyStr then
    Enum(root);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  EnumFolders('C:\windows', memo1.lines);
end;

Nincsenek megjegyzések:

Megjegyzés küldése