Sign in Join
Wiki copytemporary

FindAllFiles: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: FindAllFiles

OldNewLine
1+[[Unit]]: Lazarus [[fileutil]].
2+
3+To enable FileUtil in your project, please add LazUtils into required packages. Follow these steps:
4+
5+- Go to *Lazarus IDE Menu* \> *Project* \> *Project Inspector*
6+- In the *Project Inspector* dialog window, click *Add* \> *New Requirement*
7+- In the *New Requirement* dialog window, find *LazUtils* package then click OK.
8+
9+See also:
10+
11+- <https://lazarus-ccr.sourceforge.io/docs/lazutils/fileutil/findallfiles.html>
12+- <https://lazarus-ccr.sourceforge.io/docs/lazutils/fileutil/tfilesearcher.html>
13+
14+``` pascal
15+procedure FindAllFiles(AList: TStrings; const SearchPath: String;
16+ SearchMask: String = ''; SearchSubDirs: Boolean = True; DirAttr: Word = faDirectory);
17+
18+function FindAllFiles(const SearchPath: String; SearchMask: String = '';
19+ SearchSubDirs: Boolean = True): TStringList;
20+```
21+
22+**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.
23+
24+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 (;)]].\
25+Spaces in the mask are treated as literals.
26+
27+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.
28+
29+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.
30+
31+## Example
32+
33+``` pascal
34+uses
35+ ..., FileUtil, ...
36+var
37+ PascalFiles: TStringList;
38+begin
39+ PascalFiles := TStringList.Create;
40+ try
41+ FindAllFiles(PascalFiles, LazarusDirectory, '*.pas;*.pp;*.p;*.inc', true); //find e.g. all pascal sourcefiles
42+ ShowMessage(Format('Found %d Pascal source files', [PascalFiles.Count]));
43+ finally
44+ PascalFiles.Free;
45+ end;
46+
47+// or
48+
49+begin
50+ //No need to create the stringlist; the function does that for you
51+ PascalFiles := FindAllFiles(LazarusDirectory, '*.pas;*.pp;*.p;*.inc', true); //find e.g. all pascal sourcefiles
52+ try
53+ ShowMessage(Format('Found %d Pascal source files', [PascalFiles.Count]));
54+ finally
55+ PascalFiles.Free;
56+ end;
57+```
58+
59+## Watch out for memory leaks, esp. with the `FindAllFiles` function
60+
61+The *function* `FindAllFiles` creates the `TStringList`. This is convenient, but beware of memory leaks.
62+
63+``` pascal
64+// DON'T DO THIS - the line below means that TStringList created by `FindAllFiles` is never freed.
65+Listbox1.Items.Assign(FindAllFiles(LazarusDirectory, '*.pas;*.pp;*.p;*.inc', true));
66+```
67+
68+Fix it to this:
69+
70+``` pascal
71+List := FindAllFiles(LazarusDirectory, '*.pas;*.pp;*.p;*.inc', true);
72+try
73+ Listbox1.Items.Assign(List);
74+finally
75+ List.Free;
76+end;
77+```
78+
79+## This unit is part of LCLBase
80+
81+> **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
82+
83+---
84+*From the Free Pascal wiki, page "FindAllFiles", as it stood on 2024-04-26 after a change by Michalis, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history