Excluir itens de uma pasta
12/08/2008
0
Paulo
Posts
12/08/2008
Martins
Já tentou usar a função [b:aaaa268d84]DeleteFile[/b:aaaa268d84]?
Boa sorte e bons códigos.
12/08/2008
Carlosrm
apenas complementando a resposta do Martins, se quiser também deletar a Pasta (ou diretório, como era chamado antigamente), no Delphi 7 você pode usar a função RemoveDir. Porém, esta função só apaga diretórios que estejam vazios. Então, realmente será necessário usar o DeleteFile, como lembrou o Martins, para primeiro apagar cada um dos arquivos que estiverem na pasta/diretório a deletar. Por último, não confie plenamente na segurança dos arquivos/pasta apagados. Em algumas situações eles podem ser recuperados. Eu acho mais prudente alterar o conteúdo dos arquivos, antes de apagá-los. Se alguém recuperá-los, vai encontrar informação não aproveitável...
carlosrm
13/08/2008
Thiago Vidal
Segue o código, talvez vc tenha que adaptar alguma coisa aí.
function RecursiveDelete(FullPath: string): Boolean; var sr: TSearchRec; FullName: string; begin Result := True; if (FindFirst(FullPath + ´\*.*´, faAnyFile, sr) = 0) then try repeat FullName := IncludeTrailingPathDelimiter(FullPath) + sr.Name; if (sr.Name <> ´.´) and (sr.Name <> ´..´) then begin if ((sr.Attr and faDirectory) = 0) then Result := DeleteFile(FullName) else Result := RecursiveDelete(FullName); end; until (FindNext(sr) <> 0) or not Result; finally FindClose(sr); end; Result := Result and DirectoryExists(FullPath) and RemoveDir(FullPath); end;
14/08/2008
Paulo
14/08/2008
Martins
Sem problemas [b:f8743f04b1]Paulo[/b:f8743f04b1], mas veja q a função o utiliza.
if ((sr.Attr and faDirectory) = 0) then Result := DeleteFile(FullName)
Mesmo assim, bons códigos e boa sorte.
15/08/2008
Paulo
20/09/2022
Rafael Vilela
Segue o código, talvez vc tenha que adaptar alguma coisa aí.
[code:1:a175611496]function RecursiveDelete(FullPath: string): Boolean;
var
sr: TSearchRec;
FullName: string;
begin
Result := True;
if (FindFirst(FullPath + ´\\*.*´, faAnyFile, sr) = 0) then
try
repeat
FullName := IncludeTrailingPathDelimiter(FullPath) + sr.Name;
if (sr.Name <> ´.´) and (sr.Name <> ´..´) then
begin
if ((sr.Attr and faDirectory) = 0) then Result := DeleteFile(FullName)
else Result := RecursiveDelete(FullName);
end;
until (FindNext(sr) <> 0) or not Result;
finally
FindClose(sr);
end;
Result := Result and DirectoryExists(FullPath) and RemoveDir(FullPath);
end;
[/code:1:a175611496]
Por acaso tiveram problema para excluir diretórios com longo path?
Clique aqui para fazer login e interagir na Comunidade :)