Lonewolf Online

The website of an amateur photographer, astronomer and petrolhead.

Recursive Find Files Algorithm

Simple and very useful function to find files recursively in a given directory.

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.

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.

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;

 

Share/Save/Bookmark

 

What Others Are Saying

There are no comments for this item yet.

 

Have Your Say

 

 

 

 

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment. The authors reserve the right not to publish any comments that they believe are hateful, racist, demeaning or otherwise inappropriate.

    www.flickr.com
    This is a Flickr badge showing public photos and videos from Tim Trott. Make your own badge here.
  • Recent Additions
  • Search




  • Page copy protected against web site content infringement by Copyscape
This page was last updated on Monday, July 28th, 2008.
Unless otherwise stated, all photographs and content Copyright © 2000 - 2008 Tim Trott, All Rights Reserved. No graphics, photographs or content may be used without written permission.
Website Design and Graphics Copyright © 2005-2007 Tim Trott. home :: sitemap :: disclaimer:: contact