Advertisement
miguelhosttimer

buscar arquivo unidade c e abre diretório automático

May 17th, 2024
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 3.09 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.     procedure Button1Click(Sender: TObject);
  32.     procedure FormCreate(Sender: TObject);
  33.     procedure FormDestroy(Sender: TObject);
  34.   private
  35.     FSearchThread: TSearchThread;
  36.   public
  37.   end;
  38.  
  39. var
  40.   Form1: TForm1;
  41.  
  42. implementation
  43.  
  44. {$R *.dfm}
  45.  
  46. { TSearchThread }
  47.  
  48. constructor TSearchThread.Create(const FileName, SearchPath: string);
  49. begin
  50.   inherited Create(False);
  51.   FreeOnTerminate := True;
  52.   FFileName := FileName;
  53.   FSearchPath := SearchPath;
  54.   FDirectoriesSearched := 0;
  55. end;
  56.  
  57. function TSearchThread.FindFile(const FileName, SearchPath: string): string;
  58. var
  59.   SR: TSearchRec;
  60. begin
  61.   Result := '';
  62.   if FindFirst(SearchPath + '*' + FileName + '*', faAnyFile, SR) = 0 then
  63.   begin
  64.     Result := SearchPath + SR.Name;
  65.   end;
  66.   FindClose(SR);
  67.  
  68.   if Result = '' then
  69.   begin
  70.     if FindFirst(SearchPath + '*.*', faDirectory, SR) = 0 then
  71.     begin
  72.       repeat
  73.         if ((SR.Attr and faDirectory) <> 0) and (SR.Name <> '.') and (SR.Name <> '..') then
  74.         begin
  75.           Inc(FDirectoriesSearched);
  76.           FStatusText := 'Procurando em: ' + SearchPath + SR.Name;
  77.           Synchronize(UpdateStatus);
  78.           Result := FindFile(FileName, SearchPath + SR.Name + '\');
  79.           if Result <> '' then Break;
  80.         end;
  81.       until FindNext(SR) <> 0;
  82.     end;
  83.     FindClose(SR);
  84.   end;
  85. end;
  86.  
  87. procedure TSearchThread.Execute;
  88. begin
  89.   FFoundPath := FindFile(FFileName, FSearchPath);
  90.   Synchronize(UpdateUI);
  91. end;
  92.  
  93. procedure TSearchThread.UpdateUI;
  94. var
  95.   DirectoryPath: string;
  96. begin
  97.   if FFoundPath <> '' then
  98.   begin
  99.     Form1.Memo1.Lines.Add('O arquivo ' + FFileName + ' encontrado ' + FFoundPath);
  100.     DirectoryPath := ExtractFileDir(FFoundPath);
  101.     ShellExecute(0, 'open', PChar(DirectoryPath), nil, nil, SW_SHOWNORMAL);
  102.   end
  103.   else
  104.     Form1.Memo1.Lines.Add('O arquivo ' + FFileName + ' não encontrado.');
  105. end;
  106.  
  107. procedure TSearchThread.UpdateStatus;
  108. begin
  109.   Form1.Label1.Caption := FStatusText;
  110. end;
  111.  
  112. { TForm1 }
  113.  
  114. procedure TForm1.Button1Click(Sender: TObject);
  115. begin
  116.   FSearchThread := TSearchThread.Create(Edit1.Text, 'C:\');
  117. end;
  118.  
  119. procedure TForm1.FormCreate(Sender: TObject);
  120. begin
  121.   FSearchThread := nil;
  122. end;
  123.  
  124. procedure TForm1.FormDestroy(Sender: TObject);
  125. begin
  126.   if Assigned(FSearchThread) then
  127.   begin
  128.     FSearchThread.Terminate;
  129.     FSearchThread := nil;
  130.   end;
  131. end;
  132.  
  133. end.
  134.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement