Advertisement
miguelhosttimer

procurar arquivo especifico em qualquer lugar HD

May 15th, 2024
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.87 KB | None | 0 0
  1.   unit Unit1;
  2.  
  3. interface
  4.  
  5. uses
  6.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  7.   Vcl.Controls, Vcl.Forms, Vcl.StdCtrls, Vcl.Dialogs;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     Button1: TButton;
  12.     Edit1: TEdit;
  13.     FileOpenDialog1: TFileOpenDialog;
  14.     procedure Button1Click(Sender: TObject);
  15.   private
  16.     { Private declarations }
  17.     function FindFileInSubFolders(const FolderName, FileName: string): Boolean;
  18.   public
  19.     { Public declarations }
  20.   end;
  21.  
  22. var
  23.   Form1: TForm1;
  24.  
  25. implementation
  26.  
  27. {$R *.dfm}
  28.  
  29. function TForm1.FindFileInSubFolders(const FolderName, FileName: string): Boolean;
  30. var
  31.   SearchRec: TSearchRec;
  32. begin
  33.   if FindFirst(FolderName + FileName + '.*', faAnyFile, SearchRec) = 0 then
  34.   begin
  35.     Result := True;
  36.     FindClose(SearchRec);
  37.   end
  38.   else if FindFirst(FolderName + '*.*', faDirectory, SearchRec) = 0 then
  39.   begin
  40.     Result := False;
  41.     repeat
  42.       if ((SearchRec.Attr and faDirectory) <> 0)  and (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
  43.         if FindFileInSubFolders(FolderName + SearchRec.Name + '\', FileName) then
  44.         begin
  45.           Result := True;
  46.           Break;
  47.         end;
  48.     until FindNext(SearchRec) <> 0;
  49.     FindClose(SearchRec);
  50.   end
  51.   else
  52.     Result := False;
  53. end;
  54.  
  55. procedure TForm1.Button1Click(Sender: TObject);
  56. var
  57.   FileName, FolderName: string;
  58.   Found: Boolean;
  59. begin
  60.   FileName := Edit1.Text;
  61.   if FileOpenDialog1.Execute then
  62.   begin
  63.     FolderName := ExtractFilePath(FileOpenDialog1.FileName);
  64.     Found := FindFileInSubFolders(FolderName, FileName);
  65.     if Found then
  66.       ShowMessage('O arquivo ' + FileName + ' foi encontrado na pasta selecionada ou em suas subpastas.')
  67.     else
  68.       ShowMessage('O arquivo ' + FileName + ' não foi encontrado na pasta selecionada nem em suas subpastas.');
  69.   end;
  70. end;
  71.  
  72. end.
  73.  
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement