When developing applications that need to operate on files and directories it is a common task to, for example, get the list of all sub-directories for a given directory.
GetSubDirectories
The GetSubDirectories fills a TStrings with all the subdirectories for a given directory: //fils the "list" TStrings with the subdirectories of the "directory" directory
procedure GetSubDirectories(const directory : string; list : TStrings) ;
var
sr : TSearchRec;
begin
try
if FindFirst(IncludeTrailingPathDelimiter(directory) + '*.*', faDirectory, sr) < 0 then
Exit
else
repeat
if ((sr.Attr and faDirectory <> 0) AND (sr.Name <> '.') AND (sr.Name <> '..')) then
List.Add(IncludeTrailingPathDelimiter(directory) + sr.Name) ;
until FindNext(sr) <> 0;
finally
SysUtils.FindClose(sr) ;
end;
end;
Usage example 1:
GetSubDirectories('c:\', Memo1.Lines) ;
Usage example 2:
var
sl : TStringList;
begin
sl := TStringList.Create;
try
GetSubDirectories('c:\', sl) ;
ShowMessage('Number of subfolders in "c:\": ' + IntToStr(sl.Count)) ;
finally
FreeAndNil(sl) ;
end;
end;
Note: this function will not recurse subdirectories - it will only return a list of sub folder on the "first" level.
More on directories: Directory Related Delphi Functions and Procedures
Delphi tips navigator:
» Programmatically Fix the TabOrder property in your Delphi applications
« Dirty Delphi Localization: Hijack consts.pas to have Country / Language Specific Messages
