Author: Tomas Rutkauskas
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:
1 uses2 SysUtils;
3 4 procedure GetDirList(const Root: AnsiString; Dirs: TStrings);
5 var6 Found: TSearchRec;
7 8 function IsDirectory(F: TSearchRec): Boolean
9 begin10 Result := F.Name <> '.';
11 Result := Result or (F.Name) <> '..';
12 Result := Result and (F.Attr and faDirectory = faDirectory);
13 end;
14 15 begin16 Dirs.Clear;
17 if FindFirst(Root + '\*.*', faAnFile, Found) = 0 then18 begin19 try20 if IsDirectory(Found) then21 Dirs.Add(Root + '\' + Found.Name);
22 while FindNext(Found) = 0 do23 begin24 if IsDirectory(Found) then25 Dirs.Add(Root + '\' + Found.Name);
26 end;
27 finally28 FindFree
29 end;
30 end;
31 end;