To clarify, the second parameter to my function below (LeaveFolder)
specifies whether or not to delete the actual folder you specify, or just to
empty it of all it's files and subfolders.

The ShellAPI unit can be very useful for things like this.  The same
ShFileOperation can be used for other things like copying files, and will
throw up the standard Windows progress dialog etc, depending on which flags
you set and how long Windows reckons the operation will take.

Cheers,

Conor

-----Original Message-----
From: Conor Boyd [mailto:Conor.Boyd@;treasury.sungard.com]

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
---------------------------------------------------------------------------
    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