• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1263
  • Last Modified:

Delete folder

how do i code to delete a folder irregardless of whether it has stuff in it or not ?
0
tyfing
Asked:
tyfing
1 Solution
 
CyberGhostCommented:
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...
0
 
j42Commented:
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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
esoftbgCommented:
0
 
geobulCommented:
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
0
 
geobulCommented:
Also try this one:

procedure DeleteTree(Dir: string);
var
  Rec: TSearchRec;
begin
  if Dir[Length(Dir)] <> '\' then
    Dir:= Dir + '\';
  if FindFirst(Dir + '*.*', faAnyFile, Rec) = 0 then
  repeat
    if (Rec.Name <> '.') and (Rec.Name <> '..') then
      if Rec.Attr and faDirectory > 0 then
        DeleteTree(Dir + Rec.Name)
      else
       DeleteFile(Dir + Rec.Name);
  until FindNext(Rec) <> 0;
  RemoveDir(ExtractFileDir(Dir));
  FindClose(Rec);
end;

Regards, Geo
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now