Link to home
Start Free TrialLog in
Avatar of tyfing
tyfing

asked on

Delete folder

how do i code to delete a folder irregardless of whether it has stuff in it or not ?
Avatar of CyberGhost
CyberGhost
Flag of Czechia image

Maybe there is any API function in Windows that allow this.

But I am not sure. You can however use a for statement to determine if there is something in your directory and then delete one by one file/dir. Only if it is empty Windows will allow you to delete it.

If some1 know othe (better) way, plz let us know...
Avatar of j42
j42

I assume CyberGhost is right. First you have to delete the content of the folder. You can look up 'FindFirst' in the Delphi help for some help how to get the content of a folder.

Regards
J
Hi,

Try this:

uses shellapi;

procedure DeleteTree(Dir: String);
var
   T:TSHFileOpStruct;
   X: Integer;
begin
     with T do
     begin
          Wnd:=0;
          wFunc:=FO_DELETE;
          pFrom:=PChar(Dir);
          pTo:=nil;
          { with next line - no recycle bin }
//          fFlags:=FOF_NOCONFIRMATION  ;
     end;
     SHFileOperation(T);
end;

Usage:
DeleteTree('d:\Temp');

Regards, Geo
ASKER CERTIFIED SOLUTION
Avatar of geobul
geobul

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial