Topic: [Script] Search files

Example, how to search files in the system.
Works beginning from version 4.5


procedure FindFiles(Path: string; FileName: string; ListFiles: TStringList);
var
  Search: TSearchObj;
begin
  Search := TSearchObj.Create;
  try
      if Length(Path)>0 then
          if Path[Length(Path)]<>'\' then Path := Path + '\';

      // find all files
      if Search.FindFirst(Path+FileName, faAnyFile) = 0 then
      begin
        repeat
          // add the files to the listbox
          ListFiles.Add(Path + Search.Name);
        until Search.FindNext <> 0;
      end;

      // Subdirectories/ Unterverzeichnisse
      if Search.FindFirst(Path + '*.*', faDirectory) = 0 then
      begin
        repeat
          if ((Search.Attr and faDirectory) = faDirectory) and (Search.Name[1] <> '.') then
            FindFiles(Path + Search.Name + '\', FileName, ListFiles);
        until Search.FindNext <> 0;
        Search.FindClose;
      end;

  finally
      Search.Free;
  end;
end;


procedure Form1_Button1_OnClick (Sender: TObject; var Cancel: boolean);
var
    Files: TStringList;
begin
    Files := TStringList.Create;
    FindFiles('c:\folder\', 'filename.ext', Files); // also you can use mask for file name, like *.exe, or *.* for all files
    Form1.Memo1.Text := Files.Text;
    Files.Free;
end;


Project example:

Post's attachments

Attachment icon Search files.zip 4.84 kb, 773 downloads since 2018-05-23 

Dmitry.