12

when I have subfolder in folder - this code isn't delete folders... Is there any error?

procedure TForm.Remove(Dir: String);
var
  Result: TSearchRec; Found: Boolean;
begin
  Found := False;
  if FindFirst(Dir + '\*', faAnyFile, Result) = 0 then
    while not Found do begin
      if (Result.Attr and faDirectory = faDirectory) AND (Result.Name <> '.') AND (Result.Name <> '..') then Remove(Dir + '\' + Result.Name)
      else if (Result.Attr and faAnyFile <> faDirectory) then DeleteFile(Dir + '\' + Result.Name);
      Found := FindNext(Result) <> 0;
    end;
  FindClose(Result); RemoveDir(Dir);
end;
Andreas Rejbrand
  • 105,602
  • 8
  • 282
  • 384
Emi
  • 123
  • 1
  • 1
  • 4
  • 1
    As an aside, this code could be written with a repeat until loop and thus avoid the need for the `Found` local variable. – David Heffernan Apr 19 '11 at 13:09
  • 1
    Also, it's a bit funny that `Found` is `false` if a file was found, and `true` if a file was not found... – Andreas Rejbrand Apr 19 '11 at 13:16
  • If any of the answers below solved your problem, you should accept it by clicking the checkmark to the left of the answer. If more than one answer solved your problem, choose the 'best' one to accept. – Andreas Rejbrand Apr 19 '11 at 15:23
  • Some dups: https://stackoverflow.com/questions/16336761/delete-directory-with-non-empty-subdirectory-and-files, https://stackoverflow.com/questions/11798783/delete-all-files-and-folders-recursively-using-delphi – Vadzim May 08 '18 at 05:27

7 Answers7

36

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

TDirectory is found in IOUtils which is quite a recent RTL addition.

The True flag is passed to the Recursive parameter which means that the contents of the directories are empied before the directory is removed, an essential part of deleting directories.


In a comment you tell us that you use Delphi 7 and so this cannot be used.

Your code looks mostly fine. However, you don't mean:

(Result.Attr and faAnyFile <> faDirectory)

I think you mean:

(Result.Attr and faDirectory <> faDirectory)

I would probably write it as follows:

procedure TMyForm.Remove(const Dir: string);
var
  Result: TSearchRec;
begin
  if FindFirst(Dir + '\*', faAnyFile, Result) = 0 then
  begin
    Try
      repeat
        if (Result.Attr and faDirectory) = faDirectory then
        begin
          if (Result.Name <> '.') and (Result.Name <> '..') then
            Remove(Dir + '\' + Result.Name)
        end
        else if not DeleteFile(Dir + '\' + Result.Name) then
          RaiseLastOSError;
      until FindNext(Result) <> 0;
    Finally
      FindClose(Result);
    End;
  end;
  if not RemoveDir(Dir) then
    RaiseLastOSError;
end;
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 11
    Please don't capitalize `End`, `Try`, and `Finally`! – Andreas Rejbrand Apr 19 '11 at 13:17
  • 1
    @Andreas It's too late! I do this to make them stand out because they have non-standard control flow. They are like posh gotos. – David Heffernan Apr 19 '11 at 13:20
  • I think this version has a bug, it will try to delete a "file" with "." or ".." in it. All gets down to the missing begin..end after `if (Result.Attr and faDirectory) = faDirectory`, Remy got it right in his version. – Wodzu May 20 '14 at 08:07
  • @Wodzu Thank you. You are right. This is why I never ever write code like this. At my work place we use begin/end on all blocks. – David Heffernan May 20 '14 at 08:17
  • Glad I could help, it is very easy to fall in that trap without begin/end :) – Wodzu May 20 '14 at 10:53
30

If I were you, I'd just tell the operating system to delete the folder with all of its content. Do so by writing (uses ShellAPI)

var
  ShOp: TSHFileOpStruct;
begin
  ShOp.Wnd := Self.Handle;
  ShOp.wFunc := FO_DELETE;
  ShOp.pFrom := PChar('C:\Users\Andreas Rejbrand\Desktop\Test\'#0);
  ShOp.pTo := nil;
  ShOp.fFlags := FOF_NO_UI;
  SHFileOperation(ShOp);

[If you do

  ShOp.fFlags := 0;

instead, you get a nice confirmation dialog. If you do

ShOp.fFlags := FOF_NOCONFIRMATION;

you don't get the confirmation dialogue, but you do get a progress bar if the operation is lengthy. Finally, if you add the FOF_ALLOWUNDO flag, you move the directory to the Waste Bin instead of permanently deleting it.

ShOp.fFlags := FOF_ALLOWUNDO;

Of course, you can combine flags as you like:

ShOp.fFlags := FOF_NOCONFIRMATION or FOF_ALLOWUNDO;

will not show any confirmation (but a progress dialog because you don't specify FOF_NO_UI) and the directory will be moved to the waste bin and not permanently deleted.]

Andreas Rejbrand
  • 105,602
  • 8
  • 282
  • 384
  • @Emi: Then declare `const FOF_NO_UI = 1556;`. – Andreas Rejbrand Apr 19 '11 at 13:45
  • `PChar('C:\Users\Andreas Rejbrand\Desktop\Test\'#0)` is rather bizarre. What's the deal? You would write `PChar(Dir)`. But if you were using a literal then you could just write `ShOp.pFrom := 'C:\Users\Andreas Rejbrand\Desktop\Test\';` – David Heffernan Apr 19 '11 at 13:48
  • 3
    @David: According to [the documentation](http://msdn.microsoft.com/en-us/library/bb762164(VS.85).aspx), the `pFrom` string must be doubly null terminated. As far as I know, without the addition of `#0` it is only guaranteed to end with a single null character. – Andreas Rejbrand Apr 19 '11 at 13:52
  • 4
    @David: [SHFIleOperation](http://msdn.microsoft.com/en-us/library/bb759795%28v=VS.85%29.aspx) expects a double-null terminated list of files or folders. The PChar cast provides the first one; the concatenation provides the second. – Ken White Apr 19 '11 at 13:52
  • 2
    +1 for using the OS to do the work... Though I would specify "no UI" depending on why I were deleting a folder, so standard confirmations can be brought up when circumstances require it. For example when it doesn't fit in the recycle bin, or there are subfolders which require additional priviliges, etc. – Marjan Venema Apr 19 '11 at 14:04
  • 1
    FWIW this methods will not work for services, only desktop apps, and it can be very slow. – Eric Grange Jun 03 '15 at 14:14
  • My version of your code was silently failing because the `PChar()` was missing. THX – Paulo França Lacerda Jan 14 '17 at 05:00
10

The last time I needed to delete a folder with content I used the JCL:

uses JclFileUtils;

DeleteDirectory(DirToDelete, True);

The last parameter tells whether the files should go to the recycle bin or not, which is a nice bonus.

Heinrich Ulbricht
  • 10,064
  • 4
  • 54
  • 85
  • And what happens if the folder and all its contents do not fit in the recycle bin? Does DeleteDirectory use the ShellApi so that dealing with these kinds of circumstances is done like the OS would do? – Marjan Venema Apr 19 '11 at 14:01
  • 1
    Behind the scenes `DeleteDirectory` does exactly what Andreas solution does: call `SHFileOperation`. But if you are already using the JCL then calling DeleteDirectory is just one convenient line of code. You should check the return value, though. – Heinrich Ulbricht Apr 19 '11 at 14:14
6

To address the original problem - try this:

procedure TForm.Remove(const Dir: String);
var
  sDir: String;
  Rec: TSearchRec;
begin
  sDir := IncludeTrailingPathDelimiter(Dir);
  if FindFirst(sDir + '*.*', faAnyFile, Rec) = 0 then
  try
    repeat
      if (Rec.Attr and faDirectory) = faDirectory then
      begin
        if (Rec.Name <> '.') and (Rec.Name <> '..') then
          Remove(sDir + Rec.Name);
      end else
      begin
        DeleteFile(sDir + Rec.Name);
      end;
    until FindNext(Rec) <> 0;
  finally
    FindClose(Rec);
  end;
  RemoveDir(sDir);
end; 
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
4
uses DSiWin32;

DSiDeleteTree(folderName, false);

DSiWin32 is open source project relased with "use as you wish" license.

gabr
  • 26,580
  • 9
  • 75
  • 141
0

2022: System.IOUtils.TDirectory.Delete :)

kazuser
  • 307
  • 3
  • 10
0

This procedure works for different formats:

procedure Remove(const FileName: string);
var
  Path: string;
  SearchRec: TSearchRec;
  procedure RemoveDirectory(const Dir: string);
  var
    SearchRec: TSearchRec;
  begin
    if FindFirst(Dir + '\*', faAnyFile, SearchRec) = 0 then
    begin
      try
        repeat
          if (SearchRec.Attr and faDirectory) = faDirectory then
          begin
            if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
              RemoveDirectory(Dir + '\' + SearchRec.Name)
          end
          else
            System.SysUtils.DeleteFile(Dir + '\' + SearchRec.Name);
        until FindNext(SearchRec) <> 0;
      finally
        System.SysUtils.FindClose(SearchRec);
      end;
    end;
    RemoveDir(Dir);
  end;

begin
  if DirectoryExists(FileName) then
    RemoveDirectory(FileName)
  else if FindFirst(FileName, faAnyFile, SearchRec) = 0 then
  begin
    repeat
      if (SearchRec.Name = '.') or (SearchRec.Name = '..') then
        Continue;

      Path := ExtractFilePath(FileName) + '\' + SearchRec.Name;
      if DirectoryExists(Path) then
        RemoveDirectory(Path)
      else
        System.SysUtils.DeleteFile(Path);

    until FindNext(SearchRec) <> 0;

    System.SysUtils.FindClose(SearchRec);
  end;
end;
  • D:\myFolder\ #remove directory
  • D:\myFolder\* #remove all files & directories
  • D:\myFolder\*.txt #remove text files
  • D:\myFolder\*lib*.dll #remove special dll files
  • D:\myFolder\readme.txt #remove file