Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is most likely no translated for the post!





Share:
Notifications
Clear all

Lazarus - Find All Files in a directory and subdirectories matching criteria  

  RSS

Hans
 Hans
(@hans)
Noble Member Admin
Joined: 7 years ago
Posts: 1298
September 11, 2013 5:22 PM  

With the usual TSearchRec and FindFirst functions (also found in Delphi) one can retrieve all files in a directory and subdirectories.

To my pleasant surprise, Lazarus has a great little function in the "fileutil" unit called FindAllFile() which does all this for you and shoves the result nicely in a TStringList ... 

uses 
...
fileutil
...
var
PascalFiles: TStringList;
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;

ReplyQuote
Hans
 Hans
(@hans)
Noble Member Admin
Joined: 7 years ago
Posts: 1298
September 16, 2019 11:24 PM  

Note: 

FindAllFiles also allows the use of a rather poorly documented parameter "DirAttr".
With this parameter you can determine what kind of "files" will be returned.

Constant        Value   Description       Linux Meaning
faReadOnly 1 Read-only files Current user does not have write access. 
faHidden 2 Hidden files File name begins with ".". 
faSysFile 4 System files File is socket, symbolic link, device file, or FIFO. 
faVolumeID 8 Volume ID files Not used. 
faDirectory 16 Directory files Directory. 
faArchive 32 Archive files Not used. 
faSymLink 64 Symbolic link File is a symbolic link. 
faAnyFile 71 Any file Any file. 

So if you want only directories, set this value to faDirectory (or 16).
If you want everything but directories, set this to 182 (1+2+4+8+32+64+71).

p.s. there is also a FindAllDirectories functions, which would have been just as easy of course. But the DirAttr can be used for other purposes as well, for example for listing on hidden files, etc.


ReplyQuote
Share: