Deletar todos arquivos e subdiretórios do diretório
// Dica para deletar todos arquivos e subdiretórios do diretório "Temp" do Windows
function GetTempDir: string; // Retorna o Diretorio Temp do Windows var TempDir: array[0..255] of Char;
nSize:integer; begin
SetLength(Result,255);
nSize:=GetTempPath(255, PChar(Result));
SetLength(Result,nSize); if Result[Length(Result)]<>'' then Result:= Result+''; end;
procedure NewZapFiles(StDelDir: boolean; Caminho: string); var SR: TSearchRec;
Count: integer; begin if DirectoryExists(ExtractFilePath(Caminho))=False then
ShowMessage('Diretório Inexistente!!!') else begin
Count:= FindFirst(Caminho,faAnyFile,SR); while Count=0 do begin //Application.ProcessMessages; if (SR.Attr= faDirectory) then begin if (SR.Name<>'.') and (SR.Name <> '..') then
NewZapFiles(True,ExtractFilePath(Caminho)+SR.Name+'*.*') endelse begin if FileGetAttr(ExtractFilePath(Caminho)+SR.Name)>0 then
FileSetAttr(ExtractFilePath(Caminho)+SR.Name, 0);
DeleteFile(ExtractFilePath(Caminho)+SR.Name); end;
Count:= FindNext(SR); end;
FindClose(SR); if StDelDir= True then RemoveDir(ExtractFilePath(Caminho)); end; end;
// Acrescente um "Button" e para o evento "OnClick" utilize o código de comandos abaixo:
procedure TForm1.Button1Click(Sender: TObject); begin if MessageDlg('Deseja realmente deletar arquivos temporários do Windows?'+#13+'('+GetTempDir+'*.*)', mtConfirmation, [mbYes, mbNo], 0) = mrYes then
NewZapFiles(False,GetTempDir+'*.*'); end;