[Overview][Constants][Types][Classes][Procedures and functions][Index] Reference for unit 'FileUtil' (#lazutils)

FindAllFiles

Returns the list of files in the specified path matching the search criteria.

Declaration

Source position: fileutil.pas line 179

function FindAllFiles(

  const SearchPath: string;

  const SearchMask: string = '';

  SearchSubDirs: Boolean = True;

  DirAttr: Word = faDirectory;

  MaskSeparator: Char = ';';

  PathSeparator: Char = ';'

):TStringList; overload;

procedure FindAllFiles(

  AList: TStrings;

  const SearchPath: string;

  const SearchMask: string = '';

  SearchSubDirs: Boolean = True;

  DirAttr: Word = faDirectory;

  MaskSeparator: Char = ';';

  PathSeparator: Char = ';'

); overload;

Arguments

SearchPath

  

Base path for searching files.

SearchMask

  

A list of file masks, separated by a semicolon (;), used to determine which files are a match in the routine. The mask can contain wildcards like * and ? and it also supports sets like [a-d,x]. See the Masks unit for more details. The default value is an empty string ('') and causes the all files mask for the platform to be used.

SearchSubDirs

  

True to search for matching files in subdirectories.

DirAttr

  

Specifies the file attribute which indicates whether a file system entry is treated as a directory. It can contain faDirectory, faSymLink, (faDirectory+ faSymLink), or other bits can be used. The default value is faDirectory.

MaskSeparator

  

Separator used between file masks in the SearchMask argument. Default value is ';'.

PathSeparator

  

Separator used between path names in the SearchPath argument. Default value is ';'.

Function result

TStringList instance with the file names matching the search criteria. The StringList is created in the FindAllFiles function; you should not instantiate it before calling the function, and it must be freed in the calling routine.

Arguments

AList

  

TStringList used to store file names matching the search criteria. AList must be instantiated before it is passed as an argument to the method. The TStringList instance must also be freed by the routine where it was created.

SearchPath

  

Base path for searching files.

SearchMask

  

A list of file masks, separated by a semicolon (;), used to determine which files are a match in the routine. The mask can contain wildcards like * and ? and it also supports sets like [a-d,x]. See the Masks unit for more details. The default value is an empty string ('') and causes the all files mask for the platform to be used.

SearchSubDirs

  

True to search for matching files in subdirectories.

DirAttr

  

Specifies the file attribute which indicates whether a file system entry is treated as a directory. It can contain faDirectory, faSymLink, (faDirectory+ faSymLink), or other bits can be used. The default value is faDirectory.

MaskSeparator

  

Separator used between file masks in the SearchMask argument. Default value is ';'.

PathSeparator

  

Separator used between path names in the SearchPath argument. Default value is ';'.

Description

FindAllFiles is a routine used to gather a list of file names found in the specified search paths which match the specified file masks. Overloaded variants are provided which allow the list of matching file names to be returned in different manners.

The procedure variant uses an existing TStringList class instance to store the matching file names. The function variant creates a new TStringList instance which is used as the return value. It creates the string list internally. This may seem very convenient at first, but it is very easy to create memory leaks if string list instances are not freed properly.

FindAllFiles uses the TListFileSearcher class to perform the file search using the specified parameter values.

See also

TListFileSearcher

  

Stores file names matching a search criteria in a TStrings class instance.

Example

uses
  FileUtil;
var
  PascalFiles: TStringList;
begin
  PascalFiles := TStringList.Create;
  try
    FindAllFiles(PascalFiles, LazarusDirectory, '*.pas;*.pp;*.p;*.inc', true); //find e.g. all pascal sourcefiles
    ShowMessage(Format('Found %d Pascal source files', [PascalFiles.Count]));
  finally
    PascalFiles.Free;
  end;
end;
// or
begin
  //No need to create the stringlist; the function does that for you
  PascalFiles := FindAllFiles(LazarusDirectory, '*.pas;*.pp;*.p;*.inc', true); //find e.g. all pascal sourcefiles
  try
    ShowMessage(Format('Found %d Pascal source files', [PascalFiles.Count]));
  finally
    PascalFiles.Free;
  end;
end;

Version 3.2 Generated 2024-02-25 Home