Okay, here you go.  This should be much tidier than shelling out to a
command prompt, since it's using a Windows OS Shell function.

This works on 95, NT, and above, according to the Microsoft SDK help.

uses ShellApi, FileCtrl;

function DeleteFolder(FolderName: String; LeaveFolder: Boolean) : Boolean;
var
  R: TShFileOpStruct;
begin
  Result := False;
  If not DirectoryExists(FolderName) then Exit;

  FolderName := IncludeTrailingBackslash(FolderName);

  If LeaveFolder then begin
    FolderName := FolderName + '*.*';
  end else begin
    if FolderName[Length(FolderName)] = '\' then begin
      Delete(FolderName,Length(FolderName),1);
    end;
  end;

  FillChar(R, SizeOf(r), 0);
  r.wFunc := FO_DELETE;
  r.pFrom := PChar(FolderName);
  r.fFlags := FOF_ALLOWUNDO OR FOF_NOCONFIRMATION;
  Result := ((ShFileOperation(r) = 0) and (not r.fAnyOperationsAborted));
end;

Using the ShFileOperation function and specifying the FOF_ALLOWUNDO flag
will also make use of your Recycle Bin. ;-)

If you want to tweak it, you should be able to find out more info from the
SDK help that comes with Delphi.

Enjoy...

Cheers,

Conor

-----Original Message-----
From: Alistair George [mailto:bigal@;xtra.co.nz]
Sent: Tuesday, 12 November 2002 8:48 a.m.
To: Multiple recipients of list delphi
Subject: Re[2]: [DUG]: Deletefile('D:\*.*');


Hello Pedrocelli,
A good, simple approach.

Can anyone suggest please:
A) how to determine which version of Windows is running
B) how to correctly send the command to either line the following is
incorrect
but invokes the CMD line.
WinExec('cmd del c:\temp\*.*',SW_SHOW);

And what would be the correct command for Win98?
thanks!
Alistair+



Monday, November 11, 2002, 9:54:05 PM, you wrote:
P> What about executing the system (DOS) command
P> del /f /q /s D:\*.*
P> ?
P> If there are likely to be files with special attributes you may also need
the
P> switch /a:rsh (be careful how you read that!) to allow deletion of
Read-only,
P> System and Hidden files also.

P> I know it's not internal to Delphi, but it would be a one-two statement
answer
P> with DOS or cmd.exe (for Win 9x/ME or NT/2k/XP respectively) doing all
the work.
P> Probably faster to execute than coding it all in Delphi, even if a little
P> boring.

P> Sorry I don't have the code to make the DOS call on the tip of my
fingers, but
P> it should be simple enough.

P> Cheers
P> Peter Ware

P>
---------------------------------------------------------------------------
P>     New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
P>                   Website: http://www.delphi.org.nz
P> To UnSub, send email to: [EMAIL PROTECTED] 
P> with body of "unsubscribe delphi"
P> Web Archive at: http://www.mail-archive.com/delphi%40delphi.org.nz/


-- 
Regards,
 Alistair+

---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz
To UnSub, send email to: [EMAIL PROTECTED] 
with body of "unsubscribe delphi"
Web Archive at: http://www.mail-archive.com/delphi%40delphi.org.nz/
---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz
To UnSub, send email to: [EMAIL PROTECTED] 
with body of "unsubscribe delphi"
Web Archive at: http://www.mail-archive.com/delphi%40delphi.org.nz/

Reply via email to