Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   Borrar un directorio con contenido (https://www.clubdelphi.com/foros/showthread.php?t=3663)

Mariana 16-09-2003 02:58:03

Borrar un directorio con contenido
 
Hola a todos, esta vez mi pregunta es si existe algún comando para borrar un directorio con todo su contenido, ya que con rmdir(), solo me permite borrar si no contiene nada.
Muchas Gracias.
Mariana

marcoszorrilla 16-09-2003 08:39:18

Prueba con esta función:
Código:

procedure Tselecdir.borrarClick(Sender: TObject);
  procedure borradentro(directorio:string);
  var
    n:integer;
    Rbusqueda : TSearchRec;
  begin
    n := FindFirst(directorio+'\*.*',faanyfile, Rbusqueda);
    while n=0 do begin
        if (rbusqueda.Name <> '..') and
            (rbusqueda.Name <> '.') then begin
            if (rbusqueda.Attr = faDirectory) then begin
                borradentro(directorio+'\'+rbusqueda.Name);
                rmdir(directorio+'\'+rbusqueda.Name);
            end else
                deletefile(directorio+'\'+Rbusqueda.name) ;
        end;
        n := FindNext(Rbusqueda);
    end;

  end;
var
  diractivo:string;
begin
    //hago el de defecto el padre, sino.. error I/O16
  chdir('..');
  GetDir(0,diractivo);
  borradentro(DirectLB.directory );
  rmdir(DirectLB.directory);
  DirectLB.directory := diractivo;
end;

Un Saludo.

CFPA86 16-09-2003 20:44:56

Hola Mariana, si el codigo anterior no te funciona pruebe este.

procedure TForm1.Button1Click(Sender: TObject);
var
DirInfo: TSearchRec;
r : Integer;
begin
r := FindFirst('C:\Download\Test\*.*', FaAnyfile, DirInfo);
while r = 0 do begin
if ((DirInfo.Attr and FaDirectory <> FaDirectory) and
(DirInfo.Attr and FaVolumeId <> FaVolumeID)) then
if DeleteFile(pChar('C:\Download\test\' + DirInfo.Name))
= false then
{Si no puede borrar el fichero}
ShowMessage('Unable to delete : C:\Download\test\' +
DirInfo.Name);
r := FindNext(DirInfo);
end;
SysUtils.FindClose(DirInfo);
if RemoveDirectory('C:\Download\Test') = false then
{Si no puedes borrar el directorio}
ShowMessage('Unable to delete dirctory : C:\Download\test');
end;

Espero te sirva.

delphi.com.ar 15-09-2004 15:18:13

Solo para agregar un poco de código al "repositorio" del foro, recién desarrollé esto:
Código Delphi [-]
procedure DeleteDir(APath: string);
var
  sr: TSearchRec;
begin
   APath := IncludeTrailingBackslash(APath);
   if FindFirst(APath + '*.*', faAnyFile, sr) = 0 Then
     repeat
       if (sr.Name <> '..') and (sr.Name <> '.') then
       begin
         if (sr.Attr = faDirectory) then
           DeleteDir(APath + sr.Name)
         else
           Win32Check(SysUtils.DeleteFile(APath + sr.Name));
       end;
     until FindNext(sr) <> 0;

   SysUtils.FindClose(sr);
   APath := ExcludeTrailingBackslash(APath);
   Win32Check(RemoveDir(APath));
end;

Saludos!

roman 15-09-2004 16:59:14

Pues otro más para la colección:

Código Delphi [-]
uses ShellApi;

procedure RemoveFolder(Folder: String);
var
  FileInfo: TShFileOpStruct;

begin
  FileInfo.Wnd := 0;
  FileInfo.wFunc := FO_DELETE;
  FileInfo.pFrom := PChar(Folder);
  FileInfo.pTo := nil;
  FileInfo.fFlags := FOF_NOERRORUI or FOF_NOCONFIRMATION;

  ShFileOperation(FileInfo);
end;

// Saludos

serg 21-02-2008 00:03:53

Correción de código
 
nada que ver
disculpa

delphi.com.ar 21-02-2008 19:23:46

No entendi..... ¿hay algún cambio a mi código?

serg 22-02-2008 18:05:42

Cita:

Empezado por delphi.com.ar (Mensaje 267654)
No entendi..... ¿hay algún cambio a mi código?


NO, ES SOLO QUE ME EQUIVOQUÉ
PERDÓN.
PERO SI TENGO UN PROBLEMA.
NO PUEDO BORRAR EL DIRECTORIO SI RECIEN ESCRIBÍ UN ARCHIVO EN ÉL, SUPONGO QUE DEBO LIBERARLO DE MEMORIA ANTES, PORQUE WINDOWS ME DICE QUE HAY OTRO PROGRAMA USANDOLO. EL CODIGO QUE USÉ ES ESTE:
Cita:

codigo delphi[-]
procedure TFmain.RemoveFolder(folder:string);
var
sr: TSearchRec;
n: Integer;
begin
n:=FindFirst(folder+'\*.*', faAnyFile, sr);
while n=0 do
begin
if (sr.Name <> '..')and(sr.Name <> '.')
then
begin
if not(sr.Attr = faDirectory)
then
deletefile(folder+'\'+sr.name) ;
end;
n := FindNext(sr);
end;
SysUtils.FindClose(sr);
folder := ExcludeTrailingBackslash(folder);
if DirectoryExists(folder) then
Win32Check(RemoveDir(folder));
end;
GRACIAS DE ANTEMANO

delphi.com.ar 26-02-2008 19:45:58

No vas a poder borrar una carpeta si no puedes borrar todos los archivos contenidos por ella, y no podrás borrar un archivo si esta en uso. Si tu programa lo esta usando solo tienes que cerrar el archivo. Muchas veces sucede que hay diálogos que tienen abiertas carpetas, entonces algunas versiones de windows no te dejan borrar estos directorios por ese motivo, otra precaución puede ser cambiar el currentpath de la aplicación.

Saludos!

Saludos!

Neftali [Germán.Estévez] 27-02-2008 09:59:53

Truco 232.

NOTA: Hoy estoy vago.;)


La franja horaria es GMT +2. Ahora son las 12:48:17.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi