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;
- FFoundPath: string;
- FStatusText: string;
- FDirectoriesSearched: Integer;
- procedure UpdateUI;
- procedure UpdateStatus;
- function FindFile(const FileName, SearchPath: string): string;
- protected
- procedure Execute; override;
- public
- constructor Create(const FileName, SearchPath: string);
- end;
- Tform1 = class(TForm)
- Button1: TButton;
- Edit1: TEdit;
- Memo1: TMemo;
- Label1: TLabel;
- Button2: TButton;
- procedure Button1Click(Sender: TObject);
- procedure Button2Click(Sender: TObject); // Adicione esta linha
- procedure FormCreate(Sender: TObject);
- procedure FormDestroy(Sender: TObject);
- private
- { Private declarations }
- FSearchThread: TSearchThread;
- public
- { Public declarations }
- 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;
- end;
- function TSearchThread.FindFile(const FileName, SearchPath: string): string;
- var
- SR: TSearchRec;
- begin
- Result := '';
- if FindFirst(SearchPath + '*' + FileName + '*', faAnyFile, SR) = 0 then
- begin
- Result := SearchPath + SR.Name;
- end;
- FindClose(SR);
- if Result = '' then
- begin
- 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);
- Result := FindFile(FileName, SearchPath + SR.Name + '\');
- if Result <> '' then Break;
- end;
- until FindNext(SR) <> 0;
- end;
- FindClose(SR);
- end;
- end;
- procedure TSearchThread.Execute;
- begin
- FFoundPath := FindFile(FFileName, FSearchPath);
- Synchronize(UpdateUI);
- end;
- procedure TSearchThread.UpdateUI;
- begin
- if FFoundPath <> '' then
- Form1.Memo1.Lines.Add('O arquivo ' + FFileName + ' encontrado ' + FFoundPath)
- else
- Form1.Memo1.Lines.Add('O arquivo ' + FFileName + ' não encontrado.');
- 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.Button2Click(Sender: TObject); // Adicione este procedimento
- var
- MyFile: TextFile;
- i: Integer;
- begin
- AssignFile(MyFile, 'C:\Users\' + GetEnvironmentVariable('USERNAME') + '\Desktop\SearchResults.txt');
- Rewrite(MyFile);
- for i := 0 to Memo1.Lines.Count - 1 do
- WriteLn(MyFile, Memo1.Lines[i]);
- CloseFile(MyFile);
- 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