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;
- 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;
- begin
- if FindFirst(SearchPath + '*' + FileName + '*', faAnyFile, SR) = 0 then
- begin
- repeat
- 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
- FindFiles(FFileName, FSearchPath);
- Synchronize(UpdateUI);
- end;
- procedure TSearchThread.UpdateUI;
- var
- i: Integer;
- begin
- for i := 0 to FFoundFiles.Count - 1 do
- begin
- Form1.Memo1.Lines.Add('O arquivo ' + FFileName + ' encontrado ' + FFoundFiles[i]);
- end;
- if FFoundFiles.Count = 0 then
- Form1.Memo1.Lines.Add('O arquivo ' + FFileName + ' não encontrado.');
- // 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
- FSearchThread := TSearchThread.Create(Edit1.Text, 'C:\');
- 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