Advertisement
miguelhosttimer

renomear arquivos na pasta dowload

May 15th, 2024
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.91 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;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     Button1: TButton;
  12.     procedure Button1Click(Sender: TObject);
  13.   private
  14.     { Private declarations }
  15.   public
  16.     { Public declarations }
  17.   end;
  18.  
  19. var
  20.   Form1: TForm1;
  21.  
  22. implementation
  23.  
  24. {$R *.dfm}
  25.  
  26. procedure TForm1.Button1Click(Sender: TObject);
  27. var
  28.   SearchRec: TSearchRec;
  29.   ArquivoOriginal, NovoArquivo: string;
  30.   Extensao: string;
  31.   ExtensaoArray: array[1..10] of string;
  32.   i: Integer;
  33. begin
  34.   // Define as extensões
  35.   ExtensaoArray[1] := '.exe';
  36.   ExtensaoArray[2] := '.txt';
  37.   ExtensaoArray[3] := '.doc';
  38.   ExtensaoArray[4] := '.docx';
  39.   ExtensaoArray[5] := '.xls';
  40.   ExtensaoArray[6] := '.xlsx';
  41.   ExtensaoArray[7] := '.ppt';
  42.   ExtensaoArray[8] := '.pptx';
  43.   ExtensaoArray[9] := '.pdf';
  44.   ExtensaoArray[10] := '.zip';
  45.  
  46.   // Define o caminho da pasta de downloads
  47.   ArquivoOriginal := 'C:\Users\' + GetEnvironmentVariable('USERNAME') + '\Downloads\';
  48.  
  49.   for i := 1 to 10 do
  50.   begin
  51.     Extensao := ExtensaoArray[i];
  52.  
  53.     // Procura por arquivos com a extensão atual na pasta de downloads
  54.     if FindFirst(ArquivoOriginal + '*' + Extensao, faAnyFile, SearchRec) = 0 then
  55.     begin
  56.       repeat
  57.         // Define o caminho do novo arquivo
  58.         NovoArquivo := ArquivoOriginal + ChangeFileExt(SearchRec.Name, '.txt');
  59.  
  60.         // Renomeia o arquivo
  61.         if RenameFile(ArquivoOriginal + SearchRec.Name, NovoArquivo) then
  62.           ShowMessage(SearchRec.Name + ' foi renomeado para ' + ExtractFileName(NovoArquivo))
  63.         else
  64.           ShowMessage('Não foi possível renomear ' + SearchRec.Name);
  65.       until FindNext(SearchRec) <> 0;
  66.  
  67.       // Libera os recursos usados pela função FindFirst
  68.       FindClose(SearchRec);
  69.     end;
  70.   end;
  71. end;
  72.  
  73. end.
  74.  
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement