Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit Unit1;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.StdCtrls, Vcl.Dialogs, Vcl.ExtCtrls, ShellAPI, System.IOUtils;
- type
- TSearchThread = class(TThread)
- private
- FFileName: string;
- FSearchPath: string;
- FFoundFiles: TStringList;
- FStatusText: string;
- FDirectoriesSearched: Integer;
- procedure UpdateUI;
- procedure UpdateStatus;
- procedure FindFiles(const FileName, SearchPath: string);
- protected
- procedure Execute; override;
- public
- constructor Create(const FileName, SearchPath: string);
- destructor Destroy; override;
- end;
- TForm1 = class(TForm)
- Button1: TButton;
- Edit1: TEdit;
- Memo1: TMemo;
- Label1: TLabel;
- Label2: TLabel; // Adicione este rótulo ao seu formulário
- procedure Button1Click(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure FormDestroy(Sender: TObject);
- private
- FSearchThread: TSearchThread;
- public
- end;
- var
- Form1: TForm1;
- implementation
- {$R *.dfm}
- { TSearchThread }
- constructor TSearchThread.Create(const FileName, SearchPath: string);
- begin
- inherited Create(False);
- FreeOnTerminate := True;
- FFileName := FileName;
- FSearchPath := SearchPath;
- FDirectoriesSearched := 0;
- FFoundFiles := TStringList.Create;
- end;
- destructor TSearchThread.Destroy;
- begin
- FFoundFiles.Free;
- inherited;
- end;
- procedure TSearchThread.FindFiles(const FileName, SearchPath: string);
- var
- SR: TSearchRec;
- FileNameWithoutExt: string;
- begin
- if FindFirst(SearchPath + '*.*', faAnyFile, SR) = 0 then
- begin
- repeat
- FileNameWithoutExt := ChangeFileExt(SR.Name, '');
- if SameText(FileNameWithoutExt, FileName) then
- FFoundFiles.Add(SearchPath + SR.Name);
- until FindNext(SR) <> 0;
- end;
- FindClose(SR);
- if FindFirst(SearchPath + '*.*', faDirectory, SR) = 0 then
- begin
- repeat
- if ((SR.Attr and faDirectory) <> 0) and (SR.Name <> '.') and (SR.Name <> '..') then
- begin
- Inc(FDirectoriesSearched);
- FStatusText := 'Procurando em: ' + SearchPath + SR.Name;
- Synchronize(UpdateStatus);
- FindFiles(FileName, SearchPath + SR.Name + '\');
- end;
- until FindNext(SR) <> 0;
- end;
- FindClose(SR);
- end;
- procedure TSearchThread.Execute;
- begin
- Synchronize(procedure begin Form1.Button1.Enabled := False; end);
- FindFiles(FFileName, FSearchPath);
- Synchronize(UpdateUI);
- Synchronize(procedure begin Form1.Button1.Enabled := True; end);
- end;
- procedure TSearchThread.UpdateUI;
- var
- i: Integer;
- SearchResults: TStringList;
- begin
- SearchResults := TStringList.Create;
- try
- for i := 0 to FFoundFiles.Count - 1 do
- begin
- Form1.Memo1.Lines.Add('O arquivo ' + FFileName + ' encontrado ' + FFoundFiles[i]);
- SearchResults.Add('O arquivo ' + FFileName + ' encontrado ' + FFoundFiles[i]);
- end;
- if FFoundFiles.Count = 0 then
- begin
- Form1.Memo1.Lines.Add('O arquivo ' + FFileName + ' não encontrado.');
- SearchResults.Add('O arquivo ' + FFileName + ' não encontrado.');
- end;
- // Salve os resultados da pesquisa e as palavras pesquisadas em um arquivo chamado a.ini na pasta de documentos
- SearchResults.Add('Palavra pesquisada: ' + FFileName);
- SearchResults.SaveToFile(TPath.Combine(TPath.GetDocumentsPath, 'a.ini'));
- finally
- SearchResults.Free;
- end;
- // Atualize o Label2 com a quantidade de arquivos encontrados
- Form1.Label2.Caption := 'Quantidade de arquivos encontrados: ' + IntToStr(FFoundFiles.Count);
- end;
- procedure TSearchThread.UpdateStatus;
- begin
- Form1.Label1.Caption := FStatusText;
- end;
- { TForm1 }
- procedure TForm1.Button1Click(Sender: TObject);
- begin
- if Edit1.Text <> '' then
- FSearchThread := TSearchThread.Create(Edit1.Text, 'C:\')
- else
- begin
- ShowMessage('Por favor, insira um nome de arquivo para pesquisar.');
- end;
- end;
- procedure TForm1.FormCreate(Sender: TObject);
- begin
- FSearchThread := nil;
- end;
- procedure TForm1.FormDestroy(Sender: TObject);
- begin
- if Assigned(FSearchThread) then
- begin
- FSearchThread.Terminate;
- FSearchThread := nil;
- end;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement