Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove a directory 2

Status
Not open for further replies.

donvittorio

Programmer
Jul 17, 2003
122
GB
Hello,

As part of my program I want to remove a directory from the hard drive that has some files in it. The Delphi function RemoveDir will not work because it will only remove the directory if it is empty.
I could iterate through all files in the directory and delete them one by one and then remove the directory, but I was wondering if anybody knew a quicker and easier way to do this?

thanks

Steve
 
You may be able to use the DOS command to do this. Your call would look something like this:
Code:
ShellExecute(Handle, 'open',
        pChar('rd'),
        pChar(sDir+ ' /s /q'),
        pchar(sStartDir), SW_HIDE);
"Handle" is the handle of the current window.
"Open" is the operation.
"pChar('rd')" is the command.
"pChar(sDir+ ' /s /q')" is the parameters - sDir is the folder you want to delete, "/s" says to delete any folders under it and "/q" says to not prompt the user to verify the delete.
"pchar(sStartDir)" is the folder containing the folder you want to delete.
"SW_HIDE" says to hide the command prompt window.

-Dell
 
Hi,

I use a handy shell function to do this :

Code:
uses Windows, Classes, Graphics, Jpeg, Sysutils, [b]ShellApi, ShlObj[/b], StrUtils;
...
 
function Shell_Erase(Owner: Integer;  WichFiles: String;  SendToRecycleBin, Confirm: Boolean): Boolean;
Const
  Aborted: Boolean = False;
var
  Struct : TSHFileOpStructA;
begin
  while pos(';',WichFiles)>0 do
  WichFiles[pos(';',WichFiles)]:=#0;
  WichFiles:=WichFiles+#0#0;
  With Struct do
    begin
     wnd         :=Owner;
     wFunc       :=FO_Delete;
     pFrom       :=PChar(WichFiles);
     pTo         :=nil;
     if not Confirm then fFlags:=FOF_NOCONFIRMATION;
     if SendToRecycleBin then fFLags:=fFlags or FOF_ALLOWUNDO //or FOF_FILESONLY
      else fFlags:=fFlags or 0 ;//or FOF_FILESONLY;
     fAnyOperationsAborted:=Aborted;
     hNameMappings:=nil;
     lpszProgressTitle:=nil;
    end;
  result:=(SHFileOperationA(Struct)=0) and (not Aborted);
end;

cheers

--------------------------------------
What You See Is What You Get
 
Daddy, does your function erase a whole directory structure with subdirs and subfiles?

-----------------------------------------------------
-"There is always a different way to solve it"

//Nordlund
 
yes indeed,

you can even choose to send the files to the recycle bin(SendToRecycleBin boolean).

--------------------------------------
What You See Is What You Get
 
Well then.... Just because the recycle bin thing....
U got a star.

-----------------------------------------------------
-"There is always a different way to solve it"

//Nordlund
 
Yes, that's worth a star.

Maybe it make a useful FAQ although I don't know if it's been asked before ...

Andrew
Hampshire, UK
 
Ok, will add a FAQ later. Tried it now and its impossible to add FAQ since I'm getting coldfusion errors...

--------------------------------------
What You See Is What You Get
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top