Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete all files and folders recursively using Delphi

I am trying to delete a folder and all of its sub-folders recursively but it is not working at all, so can someone please check the code and tell me what I am doing wrong here?

I am running this code through D7 under Windows XP

if FindFirst (FolderPath + '\*', faAnyFile, f) = 0 then
      try             
         repeat

            if (f.Attr and faDirectory) <> 0 then
              begin
                    if (f.Name <> '.') and (f.Name <> '..') then
                      begin                            
                        RemoveDir(FolderPath +'\'+ f.Name);
                      end
                    else
                      begin
                        //Call function recursively...
                        ClearFolder(FolderPath +'\'+ f.Name, mask, recursive);
                      end;
              end;

         until (FindNext (f) <> 0);
      finally
        SysUtils.FindClose (f)
      end;
end;
like image 721
MChan Avatar asked Aug 03 '12 15:08

MChan


People also ask

How do I delete a folder in Delphi?

The simplest thing to do is to call TDirectory. Delete(Dir, True) .

How do I delete a folder with all files inside it recursively?

To remove a directory and all its contents, including any subdirectories and files, use the rm command with the recursive option, -r . Directories that are removed with the rmdir command cannot be recovered, nor can directories and their contents removed with the rm -r command.

How do I delete a file in Delphi?

To delete a file, pass the name of the file to the DeleteFile function. DeleteFile returns True if it deleted the file and False if it did not (for example, if the file did not exist or if it was read-only).


2 Answers

procedure DeleteDir(const DirName: string);
var
  Path: string;
  F: TSearchRec;

begin
  Path:= DirName + '\*.*';
  if FindFirst(Path, faAnyFile, F) = 0 then begin
    try
      repeat
        if (F.Attr and faDirectory <> 0) then begin
          if (F.Name <> '.') and (F.Name <> '..') then begin
            DeleteDir(DirName + '\' + F.Name);
          end;
        end
        else
          DeleteFile(DirName + '\' + F.Name);
      until FindNext(F) <> 0;
    finally
      FindClose(F);
    end;
  end;
  RemoveDir(DirName);
end;
like image 137
kludg Avatar answered Oct 17 '22 15:10

kludg


Rather than do all this hard work yourself, I'd just use SHFileOperation:

uses
  ShellAPI;

procedure DeleteDirectory(const DirName: string);
var
  FileOp: TSHFileOpStruct;
begin
  FillChar(FileOp, SizeOf(FileOp), 0);
  FileOp.wFunc := FO_DELETE;
  FileOp.pFrom := PChar(DirName+#0);//double zero-terminated
  FileOp.fFlags := FOF_SILENT or FOF_NOERRORUI or FOF_NOCONFIRMATION;
  SHFileOperation(FileOp);
end;

For what it is worth, the problem with your code is that it doesn't ever call DeleteFile. And so the directories are never getting emptied, the calls to RemoveDir fail and so on. The lack of error checking in your code doesn't really help, but adding code to delete files would get that code in half-decent shape. You also need to take care with the recursion. You must make sure that all the children are deleted first, and then the parent container. That takes a certain degree of skill to get right. The basic approach is like this:

procedure DeleteDirectory(const Name: string);
var
  F: TSearchRec;
begin
  if FindFirst(Name + '\*', faAnyFile, F) = 0 then begin
    try
      repeat
        if (F.Attr and faDirectory <> 0) then begin
          if (F.Name <> '.') and (F.Name <> '..') then begin
            DeleteDirectory(Name + '\' + F.Name);
          end;
        end else begin
          DeleteFile(Name + '\' + F.Name);
        end;
      until FindNext(F) <> 0;
    finally
      FindClose(F);
    end;
    RemoveDir(Name);
  end;
end;

I've omitted error checking for the sake of clarity, but you should check the return values of DeleteFile and RemoveDir.

like image 32
David Heffernan Avatar answered Oct 17 '22 16:10

David Heffernan