procedure DeletaDir(const RootDir:string);
var
SearchRec: tSearchREC;
Erc:Integer;
Begin
try
{$I-}
ChDir(rootdir);
if IOResult <> 0 then
Exit;
FindFirst('*.*', faAnyFile, SearchRec);
Erc:=0;
while Erc=0 do
begin
if ((searchRec.Name <> '.') and (searchrec.Name<>'..')) then
if (SearchRec.Attr and faDirectory>0) then
DeletaDir(SearchRec.Name)
Else DeleteFile(Searchrec.Name);
Erc:=FindNext ( SearchRec);
Application.ProcessMessages;
end;
finally
If Length (RootDir)>3 then
Chdir('..');
end;
RmDir(rootDir);
{$I+}
End;
quinta-feira, 11 de setembro de 2008
Como deletar uma pasta com arquivos e subpastas dentro
Assinar:
Postar comentários (Atom)
5 comentários:
Muito boa esta procedure
lembrando que ela trabalha corretamente no windows 7 ja
as que tem no código
ShFileOperation(OpStruc);
não são mais compatíveis com
windows 7
Abraço
Função acima funcionando e melhorada!
function LimpaOuRemoveDiretorio(_sDiretorio: String; _bRemoverDiretorio: Boolean): Boolean;
var
oSearchRec : TSearchRec;
iRetorno : Integer;
begin
Result := False;
_sDiretorio := IncludeTrailingPathDelimiter(_sDiretorio);
if not(DirectoryExists(_sDiretorio)) then
Exit;
iRetorno := FindFirst(_sDiretorio + '*.*', faAnyFile, oSearchRec);
while iRetorno = 0 do begin
if (oSearchRec.Name <> '.') and (oSearchRec.Name <> '..') then
if oSearchRec.Attr = faDirectory then
LimpaOuRemoveDiretorio(_sDiretorio + oSearchRec.Name, True)
else begin
if GetFileAttributes(PWideChar(_sDiretorio + oSearchRec.Name)) > 0 then
SetFileAttributes(PWideChar(_sDiretorio + oSearchRec.Name), 0);
DeleteFile(PWideChar(_sDiretorio + oSearchRec.Name));
end;
iRetorno := FindNext(oSearchRec);
end;
FindClose(oSearchRec);
if _bRemoverDiretorio then
Result := RemoveDir(_sDiretorio)
else
Result := True;
end;
[Pascal Error] uLibrary.pas(1190): E2010 Incompatible types: 'WideChar' and 'Char'
Linha:
if GetFileAttributes(PWideChar(_sDiretorio + oSearchRec.Name)) > 0 then
Apenas para ajudar, a que uso é a seguinte:
// Usar assim: DelTree(Self.Handle, 'C:\TESTE');
function DelTree(hHandle: THandle; sPath: String): boolean;
var OpStruc: TSHFileOpStruct;
FromBuffer, ToBuffer: Array[0..255] of Char;
begin
//Apaga Pasta, Sub-Pastas e Arquivos
result := false;
sPath := Trim(sPath);
if DirectoryExists(sPath) then
begin
if sPath[Length(sPath)] = '\' then
Delete(sPath, Length(sPath), 1);
fillChar(OpStruc, Sizeof(OpStruc), 0);
FillChar(FromBuffer, Sizeof(FromBuffer), 0);
FillChar(ToBuffer, Sizeof(ToBuffer), 0);
StrPCopy(FromBuffer, sPath);
OpStruc.Wnd := hHandle;
OpStruc.wFunc := FO_DELETE;
OpStruc.pFrom := @FromBuffer;
OpStruc.pTo := @ToBuffer;
OpStruc.fFlags := FOF_NO_UI;
OpStruc.fAnyOperationsAborted := False;
OpStruc.hNameMappings := nil;
Result := ((ShFileOperation(OpStruc) = 0) and (not OpStruc.fAnyOperationsAborted));
end;
end;
Em tempo:
Não será realmente deletado e sim enviado para a lixeira do Windwos, ok?
Postar um comentário