Emi
Emi

Reputation: 133

Delphi, delete folder with content

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;

Upvotes: 13

Views: 40380

Answers (7)

Mehrdad Shoja
Mehrdad Shoja

Reputation: 57

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

Upvotes: 0

kazuser
kazuser

Reputation: 316

2022: System.IOUtils.TDirectory.Delete :)

Upvotes: 1

David Heffernan
David Heffernan

Reputation: 612964

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;

Upvotes: 36

Remy Lebeau
Remy Lebeau

Reputation: 596267

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; 

Upvotes: 6

Andreas Rejbrand
Andreas Rejbrand

Reputation: 108963

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.]

Upvotes: 30

gabr
gabr

Reputation: 26830

uses DSiWin32;

DSiDeleteTree(folderName, false);

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

Upvotes: 4

Heinrich Ulbricht
Heinrich Ulbricht

Reputation: 10372

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.

Upvotes: 10

Related Questions