Advertisement
miguelhosttimer

procurar arquivo em todo hd

May 16th, 2024
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.67 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, Vcl.ExtCtrls;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     Button1: TButton;
  12.     Edit1: TEdit;
  13.     Memo1: TMemo;
  14.     procedure Button1Click(Sender: TObject);
  15.   private
  16.     { Private declarations }
  17.     function FindFile(const FileName, SearchPath: string): string;
  18.   public
  19.     { Public declarations }
  20.   end;
  21.  
  22. var
  23.   Form1: TForm1;
  24.  
  25. implementation
  26.  
  27. {$R *.dfm}
  28.  
  29. function TForm1.FindFile(const FileName, SearchPath: string): string;
  30. var
  31.   SR: TSearchRec;
  32.   FoundPath: string;
  33. begin
  34.   FoundPath := '';
  35.   if FindFirst(SearchPath + FileName, faAnyFile, SR) = 0 then
  36.   begin
  37.     FoundPath := SearchPath + FileName;
  38.   end;
  39.   FindClose(SR);
  40.  
  41.   if FoundPath = '' then
  42.   begin
  43.     if FindFirst(SearchPath + '*.*', faDirectory, SR) = 0 then
  44.     begin
  45.       repeat
  46.         if ((SR.Attr and faDirectory) <> 0) and (SR.Name <> '.') and (SR.Name <> '..') then
  47.         begin
  48.           FoundPath := FindFile(FileName, SearchPath + SR.Name + '\');
  49.           if FoundPath <> '' then Break;
  50.         end;
  51.       until FindNext(SR) <> 0;
  52.     end;
  53.     FindClose(SR);
  54.   end;
  55.  
  56.   Result := FoundPath;
  57. end;
  58.  
  59. procedure TForm1.Button1Click(Sender: TObject);
  60. var
  61.   FileName, SearchPath, FoundPath: string;
  62. begin
  63.   FileName := Edit1.Text;
  64.   SearchPath := 'C:\';
  65.   FoundPath := FindFile(FileName, SearchPath);
  66.   if FoundPath <> '' then
  67.     Memo1.Lines.Add('O arquivo ' + FileName + ' foi encontrado em ' + FoundPath)
  68.   else
  69.     Memo1.Lines.Add('O arquivo ' + FileName + ' não foi encontrado.');
  70. end;
  71.  
  72. end.
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement