Difference between revisions of "FindAllFiles"

From Lazarus wiki
Jump to navigationJump to search
(Link to RTL version)
(16 intermediate revisions by 10 users not shown)
Line 1: Line 1:
{{Translate}}
+
{{FindAllFiles}}
  
Unit: Lazarus '''fileutil''' (UTF-8 replacements for FPC RTL code and additional file/directory handling)
+
[[Unit]]:  
 +
Lazarus [[fileutil]]
  
 
See also:
 
See also:
Line 7: Line 8:
 
* http://lazarus-ccr.sourceforge.net/docs/lcl/fileutil/tfilesearcher.html
 
* http://lazarus-ccr.sourceforge.net/docs/lcl/fileutil/tfilesearcher.html
  
<syntaxhighlight>
+
<syntaxhighlight lang="pascal">
 +
procedure FindAllFiles(AList: TStrings; const SearchPath: String;
 +
  SearchMask: String = ''; SearchSubDirs: Boolean = True; DirAttr: Word = faDirectory);
 +
 
 
function FindAllFiles(const SearchPath: String; SearchMask: String = '';
 
function FindAllFiles(const SearchPath: String; SearchMask: String = '';
 
   SearchSubDirs: Boolean = True): TStringList;
 
   SearchSubDirs: Boolean = True): TStringList;
 
</syntaxhighlight>
 
</syntaxhighlight>
  
'''findallfiles''' looks for files matching the searchmask in the SearchPath directory and if specified its children and returns a stringlist with the resulting filenames.
+
'''FindAllFiles''' looks for files matching the SearchMask in the SearchPath directory and, if specified, its subdirectories, 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,
 +
or it can consist of a list of masks, separated by a [[Semicolon|semicolon (;)]].<br>
 +
Spaces in the mask are treated as literals.
 +
 
 +
Parameter DirAttr is int file attribute: if file-system item has this attribute(s), it is considered as a directory. It can be faDirectory, faSymLink, (faDirectory+faSymLink) or maybe another bits can be used.
  
The mask can be a file name or...todo finish: can wildcards * and ? be used here?
+
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|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 ==
<syntaxhighlight>
+
 
 +
<syntaxhighlight lang="pascal">
 
uses  
 
uses  
...
+
  ..., FileUtil, ...
fileutil
 
...
 
 
var
 
var
   SVNFiles: TStringList;
+
   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
 
begin
 
   //No need to create the stringlist; the function does that for you
 
   //No need to create the stringlist; the function does that for you
   SVNFiles := FindAllFiles(FSVNDirectory, 'svn' + LazUtils.GetExeExt, true); //find e.g. all svn.exe
+
   PascalFiles := FindAllFiles(LazarusDirectory, '*.pas;*.pp;*.p;*.inc', true); //find e.g. all pascal sourcefiles
 
   try
 
   try
     if SVNFiles.Count > 0 then
+
     ShowMessage(Format('Found %d Pascal source files', [PascalFiles.Count]));
    begin
 
      // Just get first result.
 
      showmessage('Found an svn executable: '+SVNFiles.Strings[0]);
 
    end;
 
 
   finally
 
   finally
     SVNFiles.Free;
+
     PascalFiles.Free;
 
   end;
 
   end;
 +
</syntaxhighlight>
  
 +
'''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:
 +
 +
<syntaxhighlight lang="pascal">
 +
  // 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);
 
</syntaxhighlight>
 
</syntaxhighlight>
[[category:Lazarus]]
+
 
[[category:fileutil]]
+
{{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}}

Revision as of 14:36, 31 July 2020

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

Unit: Lazarus fileutil

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 subdirectories, 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.

Parameter DirAttr is int file attribute: if file-system item has this attribute(s), it is considered as a directory. It can be faDirectory, faSymLink, (faDirectory+faSymLink) or maybe another bits can be used.

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

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