Fórum Como Apagar pasta e sub pastas? #428884

16/11/2012

0

Boa tarde a todos,

Estou precisando apagar um diretório e suas subpastas, peguei esta função que no delphi 7 não gera erro de compilação mais não deletou os arquivos no XE2 não esta compilando, alguém saberia como resolver?

Erro: [DCC Error] uFDescompactar.pas(506): E2010 Incompatible types: 'PWideChar' and 'string'

Agradeço!


procedure TForm1.Button1Click(Sender: TObject);
begin
RecursiveDelete('C:\Descompactar');
end;


function TFDescompactar.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) // erro
else
Result := RecursiveDelete(FullName);
end;
until (FindNext(sr) <> 0) or not Result ;
finally
FindClose(sr); // erro
end;
Result := Result and DirectoryExists(FullPath) and RemoveDir(FullName);

end;
Itamar Souza

Itamar Souza

Responder

Posts

16/11/2012

Claudia Nogueira

Fiz umas mudanças na sua função.
Se for utilizar no Delphi7 ficaria assim:
function RecursiveDelete(FullPath: String): Boolean;
Var
  sr : TSearchRec;
  iRetorno : Integer;
begin
  Result := False;
  FullPath := IncludeTrailingPathDelimiter(FullPath);
  if not(DirectoryExists(FullPath)) then
    Exit;

  iRetorno := FindFirst(FullPath + '*.*', faAnyFile, sr);
  while iRetorno = 0 do
  begin
    if (sr.Name <> '.') and (sr.Name <> '..') then
      if sr.Attr = faDirectory then
        RecursiveDelete(FullPath + sr.Name)
      else
      begin
        if GetFileAttributes(pAnsiChar(FullPath + sr.Name)) > 0 then
          SetFileAttributes(pAnsiChar(FullPath + sr.Name), 0);
        DeleteFile(pAnsiChar(FullPath + sr.Name));
      end;
      iRetorno := FindNext(sr);
  end;
  FindClose(sr);
  Result := RemoveDir(pAnsiChar(FullPath))
end;


E no XE/XE2 assim:

function RecursiveDelete(FullPath: String): Boolean;
Var
  sr : TSearchRec;
  iRetorno : Integer;
begin
  Result := False;
  FullPath := IncludeTrailingPathDelimiter(FullPath);
  if not(DirectoryExists(FullPath)) then
    Exit;

  iRetorno := FindFirst(FullPath + '*.*', faAnyFile, sr);
  while iRetorno = 0 do
  begin
    if (sr.Name <> '.') and (sr.Name <> '..') then
      if sr.Attr = faDirectory then
        RecursiveDelete(FullPath + sr.Name)
      else
      begin
        if GetFileAttributes(PWideChar(FullPath + sr.Name)) > 0 then
          SetFileAttributes(PWideChar(FullPath + sr.Name), 0);
        DeleteFile(PWideChar(FullPath + sr.Name));
      end;
      iRetorno := FindNext(sr);
  end;
  FindClose(sr);
  Result := RemoveDir(FullPath)
end;
Responder

Gostei + 0

21/11/2012

Itamar Souza


Bom dia

Muito grato pela ajuda, deu certo, um detalhe e que o comando” FindClose(sr);” gera erro no XE2, mais mesmo assim agradeço!
Att
Responder

Gostei + 0

21/11/2012

Alisson Santos

Post está sendo concluido qualquer duvida nos informar
Responder

Gostei + 0

04/04/2013

Jair Dubik

Sobre o erro do FindClose acima apresentado.
Olhando a base da Api do Windows, a coisa aparentemente não mudou, mudou nas versões dos Delphi's X XE a forma da passagem do Handle.

É fácil de entender se você tiver duas versões de Delphis e analisar as chamadas:

Se observarmos nos Delphi's mais antigos, existia a função assim:

procedure FindClose(var F: TSearchRec);
// Onde a própria função extraia do "F" o Handle
.
.
.
Windows.FindClose(F.FindHandle);
.
.
.
End;

No XE3 isto muda. É chamada a Api do Windows diretamente, ou seja, ao invés de passar o seu objeto, você precisa passar o Handle do objeto do tipo Record.
function FindClose(hFindFile: THandle): BOOL; stdcall;

Na prática para melhor entendimento vou exemplificar:

Antes(Delphi 6):
FindClose(sr);

Agora(XE3):
FindClose(sr.FindHandle);

Um abraço a todos.

Jair Dubik

Responder

Gostei + 0

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar