Advertisement
miguelhosttimer

procurar arquivos usando Delphi

May 15th, 2024
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.61 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.Dialogs, Vcl.StdCtrls, Vcl.Dialogs, Vcl.ComCtrls;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     Button1: TButton;
  12.     Edit1: TEdit;
  13.     FileOpenDialog1: TFileOpenDialog;
  14.     ProgressBar1: TProgressBar;
  15.     procedure Button1Click(Sender: TObject);
  16.   private
  17.     { Private declarations }
  18.     function FindFileInSubFolders(const FolderName, FileName: string): Boolean;
  19.     function CountFolders(const FolderName: string): Integer;
  20.   public
  21.     { Public declarations }
  22.   end;
  23.  
  24. var
  25.   Form1: TForm1;
  26.  
  27. implementation
  28.  
  29. {$R *.dfm}
  30.  
  31. function TForm1.FindFileInSubFolders(const FolderName, FileName: string): Boolean;
  32. var
  33.   SearchRec: TSearchRec;
  34. begin
  35.   if FindFirst(FolderName + FileName + '.*', faAnyFile, SearchRec) = 0 then
  36.   begin
  37.     Result := True;
  38.     FindClose(SearchRec);
  39.   end
  40.   else if FindFirst(FolderName + '*.*', faDirectory, SearchRec) = 0 then
  41.   begin
  42.     Result := False;
  43.     repeat
  44.       if ((SearchRec.Attr and faDirectory) <> 0)  and (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
  45.       begin
  46.         ProgressBar1.Position := ProgressBar1.Position + 1;
  47.         Application.ProcessMessages;
  48.         if FindFileInSubFolders(FolderName + SearchRec.Name + '\', FileName) then
  49.         begin
  50.           Result := True;
  51.           Break;
  52.         end;
  53.       end;
  54.     until FindNext(SearchRec) <> 0;
  55.     FindClose(SearchRec);
  56.   end
  57.   else
  58.     Result := False;
  59. end;
  60.  
  61. function TForm1.CountFolders(const FolderName: string): Integer;
  62. var
  63.   SearchRec: TSearchRec;
  64. begin
  65.   Result := 0;
  66.   if FindFirst(FolderName + '*.*', faDirectory, SearchRec) = 0 then
  67.   begin
  68.     repeat
  69.       if ((SearchRec.Attr and faDirectory) <> 0)  and (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
  70.         Inc(Result);
  71.     until FindNext(SearchRec) <> 0;
  72.     FindClose(SearchRec);
  73.   end;
  74. end;
  75.  
  76. procedure TForm1.Button1Click(Sender: TObject);
  77. var
  78.   FileName, FolderName: string;
  79.   Found: Boolean;
  80. begin
  81.   FileName := Edit1.Text;
  82.   if FileOpenDialog1.Execute then
  83.   begin
  84.     FolderName := ExtractFilePath(FileOpenDialog1.FileName);
  85.     ProgressBar1.Max := CountFolders(FolderName);
  86.     ProgressBar1.Position := 0;
  87.     Found := FindFileInSubFolders(FolderName, FileName);
  88.     if Found then
  89.       ShowMessage('O arquivo ' + FileName + ' foi encontrado na pasta selecionada ou em suas subpastas.')
  90.     else
  91.       ShowMessage('O arquivo ' + FileName + ' não foi encontrado na pasta selecionada nem em suas subpastas.');
  92.   end;
  93. end;
  94.  
  95. end.
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement