[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 183

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 masks, separated by a semicolon (;) to which found files should match.

SearchSubDirs

  

True to search sub-directories in the process.

PathSeparator

  

If search recursively sub directories.

Function result

The StringList is created in the FindAllFiles function; you should not instantiate it before calling the function.

Arguments

AList

  

TStringList used to store file names matching the search criteria.

SearchPath

  

Base path for searching files.

SearchMask

  

A list of masks, separated by a semicolon (;) to which found files should match.

SearchSubDirs

  

True to search sub-directories in the process.

PathSeparator

  

If search recursively sub directories.

Description

FindAllFiles is an overloaded routine used to populate a TStringList class instance with a list of files match the specified search criteria. The procedure variant uses an existing TStringList class instance to store the matching file names. The function variant creates the TStringList class instance used as the return value for the method.

Remark: The function variant of FindAllFiles 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.

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;