Advertisement
miguelhosttimer

buscar em todo hd

May 17th, 2024
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 3.42 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,ShellAPI;
  8.  
  9. type
  10.   TSearchThread = class(TThread)
  11.   private
  12.     FFileName: string;
  13.     FSearchPath: string;
  14.     FFoundPath: string;
  15.     FStatusText: string;
  16.     FDirectoriesSearched: Integer;
  17.     procedure UpdateUI;
  18.     procedure UpdateStatus;
  19.     function FindFile(const FileName, SearchPath: string): string;
  20.   protected
  21.     procedure Execute; override;
  22.   public
  23.     constructor Create(const FileName, SearchPath: string);
  24.   end;
  25.  
  26.   Tform1 = class(TForm)
  27.     Button1: TButton;
  28.     Edit1: TEdit;
  29.     Memo1: TMemo;
  30.     Label1: TLabel;
  31.     Button2: TButton;
  32.     procedure Button1Click(Sender: TObject);
  33.     procedure Button2Click(Sender: TObject); // Adicione esta linha
  34.     procedure FormCreate(Sender: TObject);
  35.     procedure FormDestroy(Sender: TObject);
  36.  
  37.   private
  38.     { Private declarations }
  39.     FSearchThread: TSearchThread;
  40.   public
  41.     { Public declarations }
  42.   end;
  43.  
  44. var
  45.   form1: Tform1;
  46.  
  47. implementation
  48.  
  49. {$R *.dfm}
  50.  
  51. { TSearchThread }
  52.  
  53. constructor TSearchThread.Create(const FileName, SearchPath: string);
  54. begin
  55.   inherited Create(False);
  56.   FreeOnTerminate := True;
  57.   FFileName := FileName;
  58.   FSearchPath := SearchPath;
  59.   FDirectoriesSearched := 0;
  60. end;
  61.  
  62. function TSearchThread.FindFile(const FileName, SearchPath: string): string;
  63. var
  64.   SR: TSearchRec;
  65. begin
  66.   Result := '';
  67.   if FindFirst(SearchPath + '*' + FileName + '*', faAnyFile, SR) = 0 then
  68.   begin
  69.     Result := SearchPath + SR.Name;
  70.   end;
  71.   FindClose(SR);
  72.  
  73.   if Result = '' then
  74.   begin
  75.     if FindFirst(SearchPath + '*.*', faDirectory, SR) = 0 then
  76.     begin
  77.       repeat
  78.         if ((SR.Attr and faDirectory) <> 0) and (SR.Name <> '.') and (SR.Name <> '..') then
  79.         begin
  80.           Inc(FDirectoriesSearched);
  81.           FStatusText := 'Procurando em: ' + SearchPath + SR.Name;
  82.           Synchronize(UpdateStatus);
  83.           Result := FindFile(FileName, SearchPath + SR.Name + '\');
  84.           if Result <> '' then Break;
  85.         end;
  86.       until FindNext(SR) <> 0;
  87.     end;
  88.     FindClose(SR);
  89.   end;
  90. end;
  91.  
  92. procedure TSearchThread.Execute;
  93. begin
  94.   FFoundPath := FindFile(FFileName, FSearchPath);
  95.   Synchronize(UpdateUI);
  96. end;
  97.  
  98. procedure TSearchThread.UpdateUI;
  99. begin
  100.   if FFoundPath <> '' then
  101.     Form1.Memo1.Lines.Add('O arquivo ' + FFileName + ' encontrado ' + FFoundPath)
  102.   else
  103.     Form1.Memo1.Lines.Add('O arquivo ' + FFileName + ' não encontrado.');
  104. end;
  105.  
  106. procedure TSearchThread.UpdateStatus;
  107. begin
  108.   Form1.Label1.Caption := FStatusText;
  109. end;
  110.  
  111. { TForm1 }
  112.  
  113. procedure Tform1.Button1Click(Sender: TObject);
  114. begin
  115.   FSearchThread := TSearchThread.Create(Edit1.Text, 'C:\');
  116. end;
  117.  
  118. procedure Tform1.Button2Click(Sender: TObject); // Adicione este procedimento
  119. var
  120.   MyFile: TextFile;
  121.   i: Integer;
  122. begin
  123.   AssignFile(MyFile, 'C:\Users\' + GetEnvironmentVariable('USERNAME') + '\Desktop\SearchResults.txt');
  124.   Rewrite(MyFile);
  125.   for i := 0 to Memo1.Lines.Count - 1 do
  126.     WriteLn(MyFile, Memo1.Lines[i]);
  127.   CloseFile(MyFile);
  128. end;
  129.  
  130. procedure Tform1.FormCreate(Sender: TObject);
  131. begin
  132.   FSearchThread := nil;
  133. end;
  134.  
  135. procedure Tform1.FormDestroy(Sender: TObject);
  136. begin
  137.   if Assigned(FSearchThread) then
  138.   begin
  139.     FSearchThread.Terminate;
  140.     FSearchThread := nil;
  141.   end;
  142. end;
  143.  
  144. end.
  145.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement