Ver Mensaje Individual
  #1  
Antiguo 11-06-2015
alopete alopete is offline
Miembro
 
Registrado: sep 2007
Posts: 95
Reputación: 17
alopete Va por buen camino
Error al borrar carpetas

Buenas,

Trabajo con Delphi 2006 y me encuentro con este problema:

Tengo que borrar una carpeta por código con todo su contenido (he probado ya todas las funciones recursivas y demás que he encontrado en foros), pero no lo elimina hasta que cierro la aplicación. No sé si es porque se queda en la memoria o así, pero he comprobado que el current directory es C:\ para asegurarme de que no se queda en algún directorio que ya esté eliminado. El código de la función que tengo ahora es el siguiente:

Código Delphi [-]
function RemoveFolder(const Folder : string): Boolean;
var
  szFile : string;
  SearchRec: TSearchRec;
  szSearchPath : string;
  nResult : integer;
  Attributes: LongWord;
  pname: pchar;
begin
  Result := False;
  szSearchPath := Folder;
  nResult := FindFirst(szSearchPath + '\*.*', faAnyFile, SearchRec);
  try
    while nResult = 0 do
    begin
      if('.' <> SearchRec.Name[1]) then
      begin
        szFile := szSearchPath + '\' + SearchRec.Name;
        FileSetAttr(szFile, 0);
        DeleteFile(PAnsiChar(szFile));
      end;
      nResult := FindNext(SearchRec);
    end;
    Result := True;
  finally
    SysUtils.FindClose(SearchRec);
  end;
  pname := PChar(Folder);
  Attributes := GetFileAttributes(pname);
  if Attributes = $FFFFFFFF then
    raise EInOutError.Create(SysErrorMessage(GetLastError));
  if (Attributes and FILE_ATTRIBUTE_READONLY) <> 0 then
    SetFileAttributes(pname, Attributes and not FILE_ATTRIBUTE_READONLY);
  Clipboard.close;
  Clipboard.Open;
  if Windows.RemoveDirectory(pname) = False then
    raise EInOutError.Create(SysErrorMessage(GetLastError)); 
end;

Gracias de antemano.
Responder Con Cita