Page 1 of 1

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

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

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.
Examples: "MacOS X - Your question", "MS Word - Your Tip or Trick".

Please note that switching to another language when reading a post will not work!
Posts will not have a translated counterpart.




RSS Feed

Home Forums Software Development Delphi, Lazarus, Free Pascal Lazarus – Find All Files in a directory and subdirectories matching criteria

This topic contains 1 reply, has 1 voice, and was last updated by  hans 1 month ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • 1670

    hans
    Keymaster

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

    hans
    Keymaster

    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.

Viewing 2 posts - 1 through 2 (of 2 total)



You must be logged in to reply to this topic.