Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

How To

How To Operate On The Recycle Bin by Glenn9999
Posted: 2 Oct 10

This FAQ covers a couple of functions which involve the recycle bin.  The first involves deleting a file to the recycle bin, and the other involves emptying the recycle bin.

SHFileOperation (or how do I delete a file to the recycle bin?)


SHFileOperation encapsulates the file operation interface for Explorer.  This allows you to copy, move, rename, or delete files.  While there are some benefits built in to doing any of these operations, which are evident from observing the operation of Explorer and studying the documentation, the most interesting one involves the ability to delete a file to the recycle bin instead of to nothingness.

Here is how to do that.  This presents a sufficient example for calling SHFileOperation for the other functions, if you provide the proper parms.


function RecycleDelete(whndle: THandle; inpath: string): integer;
{ deletes 'inpath', removing it to the recycle bin.  You can specify a list
 of files, as long as you put #0 between the files, and double-terminate it
  with #0. }
  FileOp: TSHFileOpStructA;
  inpath := inpath + #0;
  with FileOp do
       wnd := whndle;
       pFrom := PChar(inpath);
       fAnyOperationsAborted := false;
       hNameMappings := nil;
       lpszProgressTitle := nil;
  Result := SHFileOperationA(FileOp);
 { at this point, if you allow confirmation, you can interrogate
   FileOp.fAnyOperationsAborted to determine if the user aborted your operation.

The first thing is that any string must be double-terminated.  This is why I'm assigning the #0 to inpath upon entry.  The comments describe the other features of inputting files.

FOF_ALLOWUNDO - allows going to the recycle bin (or UNDO).  Removing this makes it into a regular delete function.

FOF_NOCONFIRMATION - removes the dialog box asking if you really want to do this.

More options are seen in the documentation both for delete and the other operations depending on what you need.


This function empties the recycle bin.



function SHEmptyRecycleBin(Wnd:HWnd; pszRootPath:PChar; pwFlags:Word):Integer;
        stdcall; external 'SHELL32.DLL' name 'SHEmptyRecycleBinA';

function EmptyRecycleBin(Confirm: Boolean): integer;
    SHERB_NOPROGRESSUI = $00000002;
    SHERB_NOSOUND = $00000004;
    if Confirm then
      Result := SHEmptyRecycleBin(0, nil, 0)
      Result := SHEmptyRecycleBin(0, nil, SHERB_NOCONFIRMATION);

The parms are pretty self-explanatory.  The interesting part is the nil string (pszRootPath).  Making it nil empties recycle bins on all drives.  You can define this to be a root path (e.g. "C:\"), and it will empty only that recycle bin.

Back to Embarcadero: Delphi FAQ Index
Back to Embarcadero: Delphi Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close