Delphi, delete folder with content
Asked Answered
A

7

13

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;
Adulation answered 19/4, 2011 at 12:55 Comment(4)
As an aside, this code could be written with a repeat until loop and thus avoid the need for the Found local variable.Consequential
Also, it's a bit funny that Found is false if a file was found, and true if a file was not found...Miyasawa
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.Miyasawa
Some dups: #16337261, #11799283Gilligan
M
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.]

Miyasawa answered 19/4, 2011 at 12:59 Comment(7)
@Emi: Then declare const FOF_NO_UI = 1556;.Miyasawa
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\';Consequential
@David: According to the documentation, 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.Miyasawa
@David: SHFIleOperation expects a double-null terminated list of files or folders. The PChar cast provides the first one; the concatenation provides the second.Castiglione
+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.Frager
FWIW this methods will not work for services, only desktop apps, and it can be very slow.Giraudoux
My version of your code was silently failing because the PChar() was missing. THXUltramicroscopic
C
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;
Consequential answered 19/4, 2011 at 12:59 Comment(5)
Please don't capitalize End, Try, and Finally!Miyasawa
@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.Consequential
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.Excessive
@Excessive 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.Consequential
Glad I could help, it is very easy to fall in that trap without begin/end :)Excessive
M
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.]

Miyasawa answered 19/4, 2011 at 12:59 Comment(7)
@Emi: Then declare const FOF_NO_UI = 1556;.Miyasawa
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\';Consequential
@David: According to the documentation, 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.Miyasawa
@David: SHFIleOperation expects a double-null terminated list of files or folders. The PChar cast provides the first one; the concatenation provides the second.Castiglione
+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.Frager
FWIW this methods will not work for services, only desktop apps, and it can be very slow.Giraudoux
My version of your code was silently failing because the PChar() was missing. THXUltramicroscopic
I
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.

Imagism answered 19/4, 2011 at 13:21 Comment(2)
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?Frager
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.Imagism
P
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; 
Puryear answered 19/4, 2011 at 20:3 Comment(0)
S
4
uses DSiWin32;

DSiDeleteTree(folderName, false);

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

Stepmother answered 19/4, 2011 at 13:27 Comment(0)
M
0

2022: System.IOUtils.TDirectory.Delete :)

Montalvo answered 13/12, 2022 at 4:32 Comment(0)
S
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
Strictly answered 1/6, 2023 at 15:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.