



Com está função fica fácil limpar qualquer diretório.
Segue o código fonte da função:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | function ClearDirectory(aDirectory : String) : Boolean; var SR: TSearchRec; I: integer; begin I := FindFirst(aDirectory + '*.*', faAnyFile, SR); while I = 0 do begin if (SR.Attr and faDirectory) <> faDirectory then begin if not DeleteFile(PChar(aDirectory + SR.Name)) then begin Result := False; Exit; end; end; I := FindNext(SR); end; Result := True; end; |
Exemplo de uso:
1 2 3 4 5 6 7 | procedure TForm1.Button1Click(Sender: TObject); begin if ClearDirectory('D:\teste\') then ShowMessage('Diretório limpo com sucesso') else ShowMessage('Não foi possível limpar o Diretório !'); end; |
Veja que é importante ter a “\” no fim do diretório.