Delphi, delete the folder with the content

when I have a subfolder in a folder - this code does not delete the folders ... Is there an 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; 
+7
source share
5 answers

If I were you, I would just tell the operating system to delete the folder with all the contents. Do this 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 will get a good confirmation dialog. If you do

 ShOp.fFlags := FOF_NOCONFIRMATION; 

you will not get a confirmation dialog, but you will get a progress bar if the operation is long. Finally, if you add the FOF_ALLOWUNDO flag, you move the directory to the waste bin, rather than deleting it.

 ShOp.fFlags := FOF_ALLOWUNDO; 

Of course, you can combine the flags as you wish:

 ShOp.fFlags := FOF_NOCONFIRMATION or FOF_ALLOWUNDO; 

no confirmation will be displayed (but a progress dialog, because you do not specify FOF_NO_UI ), and the directory will be moved to the garbage bin and not permanently deleted.]

+21
source

The easiest way is to call TDirectory.Delete(Dir, True) .

TDirectory is located in IOUtils , which is a fairly recent addition to RTL.

The True flag is passed to the Recursive parameter, which means that the contents of the directories will be changed before the directory is deleted, which is an important part of deleting directories.


In a comment, you tell us that you are using Delphi 7, and therefore it cannot be used.

Your code looks mostly beautiful. However, you do not mean:

 (Result.Attr and faAnyFile <> faDirectory) 

I think you mean:

 (Result.Attr and faDirectory <> faDirectory) 

I would probably write it like this:

 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; 
+20
source

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

 uses JclFileUtils; DeleteDirectory(DirToDelete, True); 

The last parameter indicates whether files should go to the trash or not, which is a good bonus.

+7
source

To fix the original problem, try the following:

 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; 
+5
source
 uses DSiWin32; DSiDeleteTree(folderName, false); 

DSiWin32 is an open source project associated with a β€œuse as you wish” license.

+3
source

All Articles