Board index » delphi » Recursive function to search local directories

Recursive function to search local directories

I am looking to find a recursive function that can search my network.
I am looking for a certain extension on files inside a directory, and
when i find it i need the path to that file.  i tried using the
directorylistbox component but couldn't get it to work.

Sent via Deja.com
http://www.deja.com/

 

Re:Recursive function to search local directories


Example:

// Check if FileName matches FileMask
function FileNameInMask(const FileMask, FileName: String): Boolean;
var
  maxi, maxj: Integer;

  function CheckMask(i, j: Integer): Boolean;
  begin
    while (i <= maxi) and (j <= maxj) do
    begin
      if FileMask[i] = '*' then
      begin
        Inc(i);
        while (j <= maxj) do
        begin
          Result := CheckMask(i, j);
          if Result then
            Exit;
          Inc(j);
        end;
      end
      else
      if (FileMask[i] = '?') or
        (FileMask[i] = FileName[j]) then
      begin
        Inc(i);
        Inc(j);
      end
      else
        Break;
    end;
    Result := (i > maxi) and (j > maxj);
  end;

begin
  maxi := Length(FileMask);
  maxj := Length(FileName);
  Result := CheckMask(1, 1);
end;

// Get a list of all files in Path (incl. subdirs) that match FileMask
procedure FindFiles(const Path, FileMask: String; const List: TStrings);

  procedure AddFiles(const Path: String);
  var
    F: TSearchRec;
  begin
    if FindFirst(Path + '*.*', faAnyFile and not faVolumeID, F) = 0 then
    try
      repeat
        if (F.Attr and faDirectory) > 0 then
        begin
          if (F.Name <> '.') and (F.Name <> '..') then
            AddFiles(Path + F.Name + '\');
        end
        else
        if FileNameInMask(FileMask, F.Name) then
          List.Add(Path + F.Name);
      until FindNext(F) <> 0;
    finally
      FindClose(F);
    end;
  end;

begin
  List.Clear;
  AddFiles(Path);
end;

begin
  // Example: get a list of all text files on the C-drive
  FindFiles('C:\', '*.txt', Memo1.Lines);
end;

<bdogg8...@my-deja.com> schreef in bericht
news:93htuu$rgn$1@nnrp1.deja.com...

Quote
> I am looking to find a recursive function that can search my network.
> I am looking for a certain extension on files inside a directory, and
> when i find it i need the path to that file.  i tried using the
> directorylistbox component but couldn't get it to work.

> Sent via Deja.com
> http://www.deja.com/

Re:Recursive function to search local directories


Check out
http://delphi.about.com/compute/delphi/library/weekly/aa051600a.htm

In article <93htuu$rg...@nnrp1.deja.com>,

Quote
  bdogg8...@my-deja.com wrote:
> I am looking to find a recursive function that can search my network.
> I am looking for a certain extension on files inside a directory, and
> when i find it i need the path to that file.  i tried using the
> directorylistbox component but couldn't get it to work.

> Sent via Deja.com
> http://www.deja.com/

Sent via Deja.com
http://www.deja.com/

Other Threads