Recursive Find Files in Dephi

Simple and very useful Delphi and Pascal function to Recursive Find Files in Dephi from a given starting directory or drive.

By Tim TrottLegacy Code • February 10, 2005
Recursive Find Files in Dephi

This function will recursively scan a folder and execute a function each time a file is found. This function will be run each time a file is found.

pascal
Function LogFiles( Const path: String; Const SRec: TSearchRec ): Boolean;
Begin
  Listbox1.Items.Add( path+SRec.Name );
  Result := True;
End;

And this is the main function called with four parameters. FindRecursive('c:/path/to/files/', '*.ext', LogFiles, TRUE)

LogFiles is the name of the above function, and the last parameter decides whether to recurse or not.

pascal
procedure FindRecursive(const path: string; const mask: string; LogFunction: TLogFunct; Re_curse: Boolean);
var
  fullpath: string;

  function Recurse(var path: string; const mask: string; Re_curse: Boolean): Boolean;
  var
    SRec: TSearchRec;
    retval: Integer;
    oldlen: Integer;
  begin
    //Recurse := FALSE;
    Recurse := Re_Curse;
    oldlen := Length(path);
    retval := FindFirst(path + mask, faAnyFile, SRec);
    while retval = 0 do
    begin
      if (SRec.Attr and (faDirectory or faVolumeID)) = 0 then
        if not LogFunction(path, SRec) then
        begin
          Result := False;
          Break;
        end;
      retval := FindNext(SRec);
    end;
    FindClose(SRec);
    if not Result then Exit;
    retval := FindFirst(path + '*.*', faDirectory, SRec);
    while retval = 0 do
    begin
      if (SRec.Attr and faDirectory) <> 0 then
        if (SRec.Name <> '.') and (SRec.Name <> '..') then
        begin
          path := path + SRec.Name + '/';
          if not Recurse(path, mask, Re_curse) then
          begin
            Result := False;
            Break;
          end;
          Delete(path, oldlen + 1, 255);
        end;
      retval := FindNext(SRec);
    end;
    FindClose(SRec);
  end;
begin
  if path = '' then
    GetDir(0, fullpath)
  else
    fullpath := path;
  if fullpath[Length(fullpath)] <> '/' then
    fullpath := fullpath + '/';
  if mask = '' then
    Recurse(fullpath, '*.*', Re_curse)
  else
    Recurse(fullpath, mask, Re_curse);
end;

About the Author

Tim Trott is a senior software engineer with over 20 years of experience in designing, building, and maintaining software systems across a range of industries. Passionate about clean code, scalable architecture, and continuous learning, he specialises in creating robust solutions that solve real-world problems. He is currently based in Edinburgh, where he develops innovative software and collaborates with teams around the globe.

Related ArticlesThese articles may also be of interest to you

CommentsShare your thoughts in the comments below

My website and its content are free to use without the clutter of adverts, popups, marketing messages or anything else like that. If you enjoyed reading this article, or it helped you in some way, all I ask in return is you leave a comment below or share this page with your friends. Thank you.

There are no comments yet. Why not get the discussion started?

New comments for this post are currently closed.