Difference between revisions of "FindAllFiles"

From Free Pascal wiki
Jump to navigationJump to search
Line 1: Line 1:
 
{{FindAllFiles}}
 
{{FindAllFiles}}
  
Unit:  
+
[[Unit]]:  
Lazarus [[LazFileUtils]] (UTF-8 replacements for FPC RTL code and additional file/directory handling)
+
Lazarus [[LazFileUtils]] ([[UTF-8]] replacements for FPC [[RTL]] code and additional file/directory handling)
  
 
See also:
 
See also:
Line 16: Line 16:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
'''findallfiles''' looks for files matching the searchmask in the SearchPath directory and if specified its children and populates a stringlist with the resulting filenames.
+
'''findallfiles''' looks for files matching the searchmask in the SearchPath directory and if specified its children and populates a [[TStrings|stringlist]] with the resulting filenames.
  
 
The mask can be a single mask like you can use with the FindFirst/FindNext functions,
 
The mask can be a single mask like you can use with the FindFirst/FindNext functions,
or it can consist of a list of masks, separated by a semicolon (;).<br>
+
or it can consist of a list of masks, separated by a [[Semicolon|semicolon (;)]].<br>
 
Spaces in the mask are treated as literals.
 
Spaces in the mask are treated as literals.
  
There are two overloaded versions of this routine. The first one is a '''procedure''' and assumes that the receiving stringlist already has been created.
+
There are two overloaded versions of this routine. The first one is a '''[[Procedure|procedure]]''' and assumes that the receiving stringlist already has been created.
The second one is a '''function''' which creates the stringlist internally and returns it as a function result. In both cases the stringlist must be destroyed by the calling procedure.
+
The second one is a '''[[Function|function]]''' which creates the stringlist internally and returns it as a function result. In both cases the stringlist must be destroyed by the calling procedure.
  
 
'''Example:'''
 
'''Example:'''

Revision as of 12:27, 6 September 2017

English (en) español (es) suomi (fi) français (fr) polski (pl) русский (ru)

Unit: Lazarus LazFileUtils (UTF-8 replacements for FPC RTL code and additional file/directory handling)

See also:

procedure FindAllFiles(AList: TStrings; const SearchPath: String;
  SearchMask: String = ''; SearchSubDirs: Boolean = True; DirAttr: Word = faDirectory); 

function FindAllFiles(const SearchPath: String; SearchMask: String = '';
  SearchSubDirs: Boolean = True): TStringList;

findallfiles looks for files matching the searchmask in the SearchPath directory and if specified its children and populates a stringlist with the resulting filenames.

The mask can be a single mask like you can use with the FindFirst/FindNext functions, or it can consist of a list of masks, separated by a semicolon (;).
Spaces in the mask are treated as literals.

There are two overloaded versions of this routine. The first one is a procedure and assumes that the receiving stringlist already has been created. The second one is a function which creates the stringlist internally and returns it as a function result. In both cases the stringlist must be destroyed by the calling procedure.

Example:

uses 
  ..., LazFileUtils, ...
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;

// 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;

IMPORTANT NOTE: The function "FindAllFiles" creates the stringlist internally. This may look very convenient at first sight, but it is very easy to create memory leaks that way:

  // DON'T EVER DO THIS !!!! - There is no way to destroy the stringlist created by FindAllFiles.
  Listbox1.Items.Assign(FindAllFiles(LazarusDirectory, '*.pas;*.pp;*.p;*.inc', true);
Light bulb  Note: If you want to use this function in command line programs, add a project requirement for LCLBase, which will not pull in the entire LCL