Advertisement
miguelhosttimer

buscar hd refinado 2

May 20th, 2024
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 4.24 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, System.IOUtils;
  8.  
  9. type
  10.   TSearchThread = class(TThread)
  11.   private
  12.     FFileName: string;
  13.     FSearchPath: string;
  14.     FFoundFiles: TStringList;
  15.     FStatusText: string;
  16.     FDirectoriesSearched: Integer;
  17.     procedure UpdateUI;
  18.     procedure UpdateStatus;
  19.     procedure FindFiles(const FileName, SearchPath: string);
  20.   protected
  21.     procedure Execute; override;
  22.   public
  23.     constructor Create(const FileName, SearchPath: string);
  24.     destructor Destroy; override;
  25.   end;
  26.  
  27.   TForm1 = class(TForm)
  28.     Button1: TButton;
  29.     Edit1: TEdit;
  30.     Memo1: TMemo;
  31.     Label1: TLabel;
  32.     Label2: TLabel;  // Adicione este rótulo ao seu formulário
  33.     procedure Button1Click(Sender: TObject);
  34.     procedure FormCreate(Sender: TObject);
  35.     procedure FormDestroy(Sender: TObject);
  36.   private
  37.     FSearchThread: TSearchThread;
  38.   public
  39.   end;
  40.  
  41. var
  42.   Form1: TForm1;
  43.  
  44. implementation
  45.  
  46. {$R *.dfm}
  47.  
  48. { TSearchThread }
  49.  
  50. constructor TSearchThread.Create(const FileName, SearchPath: string);
  51. begin
  52.   inherited Create(False);
  53.   FreeOnTerminate := True;
  54.   FFileName := FileName;
  55.   FSearchPath := SearchPath;
  56.   FDirectoriesSearched := 0;
  57.   FFoundFiles := TStringList.Create;
  58. end;
  59.  
  60. destructor TSearchThread.Destroy;
  61. begin
  62.   FFoundFiles.Free;
  63.   inherited;
  64. end;
  65.  
  66. procedure TSearchThread.FindFiles(const FileName, SearchPath: string);
  67. var
  68.   SR: TSearchRec;
  69.   FileNameWithoutExt: string;
  70. begin
  71.   if FindFirst(SearchPath + '*.*', faAnyFile, SR) = 0 then
  72.   begin
  73.     repeat
  74.       FileNameWithoutExt := ChangeFileExt(SR.Name, '');
  75.       if SameText(FileNameWithoutExt, FileName) then
  76.         FFoundFiles.Add(SearchPath + SR.Name);
  77.     until FindNext(SR) <> 0;
  78.   end;
  79.   FindClose(SR);
  80.  
  81.   if FindFirst(SearchPath + '*.*', faDirectory, SR) = 0 then
  82.   begin
  83.     repeat
  84.       if ((SR.Attr and faDirectory) <> 0) and (SR.Name <> '.') and (SR.Name <> '..') then
  85.       begin
  86.         Inc(FDirectoriesSearched);
  87.         FStatusText := 'Procurando em: ' + SearchPath + SR.Name;
  88.         Synchronize(UpdateStatus);
  89.         FindFiles(FileName, SearchPath + SR.Name + '\');
  90.       end;
  91.     until FindNext(SR) <> 0;
  92.   end;
  93.   FindClose(SR);
  94. end;
  95.  
  96. procedure TSearchThread.Execute;
  97. begin
  98.   Synchronize(procedure begin Form1.Button1.Enabled := False; end);
  99.   FindFiles(FFileName, FSearchPath);
  100.   Synchronize(UpdateUI);
  101.   Synchronize(procedure begin Form1.Button1.Enabled := True; end);
  102. end;
  103.  
  104. procedure TSearchThread.UpdateUI;
  105. var
  106.   i: Integer;
  107.   SearchResults: TStringList;
  108. begin
  109.   SearchResults := TStringList.Create;
  110.   try
  111.     for i := 0 to FFoundFiles.Count - 1 do
  112.     begin
  113.       Form1.Memo1.Lines.Add('O arquivo ' + FFileName + ' encontrado ' + FFoundFiles[i]);
  114.       SearchResults.Add('O arquivo ' + FFileName + ' encontrado ' + FFoundFiles[i]);
  115.     end;
  116.     if FFoundFiles.Count = 0 then
  117.     begin
  118.       Form1.Memo1.Lines.Add('O arquivo ' + FFileName + ' não encontrado.');
  119.       SearchResults.Add('O arquivo ' + FFileName + ' não encontrado.');
  120.     end;
  121.  
  122.     // Salve os resultados da pesquisa e as palavras pesquisadas em um arquivo chamado a.ini na pasta de documentos
  123.     SearchResults.Add('Palavra pesquisada: ' + FFileName);
  124.     SearchResults.SaveToFile(TPath.Combine(TPath.GetDocumentsPath, 'a.ini'));
  125.   finally
  126.     SearchResults.Free;
  127.   end;
  128.  
  129.   // Atualize o Label2 com a quantidade de arquivos encontrados
  130.   Form1.Label2.Caption := 'Quantidade de arquivos encontrados: ' + IntToStr(FFoundFiles.Count);
  131. end;
  132.  
  133. procedure TSearchThread.UpdateStatus;
  134. begin
  135.   Form1.Label1.Caption := FStatusText;
  136. end;
  137.  
  138. { TForm1 }
  139.  
  140. procedure TForm1.Button1Click(Sender: TObject);
  141. begin
  142.   if Edit1.Text <> '' then
  143.     FSearchThread := TSearchThread.Create(Edit1.Text, 'C:\')
  144.   else
  145.   begin
  146.     ShowMessage('Por favor, insira um nome de arquivo para pesquisar.');
  147.   end;
  148. end;
  149.  
  150. procedure TForm1.FormCreate(Sender: TObject);
  151. begin
  152.   FSearchThread := nil;
  153. end;
  154.  
  155. procedure TForm1.FormDestroy(Sender: TObject);
  156. begin
  157.   if Assigned(FSearchThread) then
  158.   begin
  159.     FSearchThread.Terminate;
  160.     FSearchThread := nil;
  161.   end;
  162. end;
  163.  
  164. end.
  165.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement