2007. augusztus 2., csütörtök

Retrieve all directories contained in a path string


Problem/Question/Abstract:

Suppose I have a path string, for example: 'c:\programs\borland\Delphi' , how can I retrieve each single directory name contained in the path (creating a tree of directories)?

Answer:

uses
  SysUtils;

procedure GetDirList(const Root: AnsiString; Dirs: TStrings);
var
  Found: TSearchRec;

  function IsDirectory(F: TSearchRec): Boolean
  begin
    Result := F.Name <> '.';
    Result := Result or (F.Name) <> '..';
    Result := Result and (F.Attr and faDirectory = faDirectory);
  end;

begin
  Dirs.Clear;
  if FindFirst(Root + '\*.*', faAnFile, Found) = 0 then
  begin
    try
      if IsDirectory(Found) then
        Dirs.Add(Root + '\' + Found.Name);
      while FindNext(Found) = 0 do
      begin
        if IsDirectory(Found) then
          Dirs.Add(Root + '\' + Found.Name);
      end;
    finally
      FindFree
    end;
  end;
end;

Nincsenek megjegyzések:

Megjegyzés küldése