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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How To Operate On The Recycle Bin

How To

How To Operate On The Recycle Bin

by  Glenn9999  Posted    (Edited  )
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?)

[link http://msdn.microsoft.com/en-us/library/bb762164%28VS.85%29.aspx]SHFileOperation[/link]

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.
Code:
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. }
var
  FileOp: TSHFileOpStructA;
begin
  inpath := inpath + #0;
  with FileOp do
     begin
       wnd := whndle;
       wFunc := FO_DELETE;  //FO_COPY, FO_DELETE, FO_MOVE, FO_RENAME
       pFrom := PChar(inpath);
       fFlags := FOF_ALLOWUNDO or FOF_NOCONFIRMATION;
       fAnyOperationsAborted := false;
       hNameMappings := nil;
       lpszProgressTitle := nil;
     end;
  Result := SHFileOperationA(FileOp);
 { at this point, if you allow confirmation, you can interrogate
   FileOp.fAnyOperationsAborted to determine if the user aborted your operation.
   }
end;

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.

SHEmptyRecycleBin

This function empties the recycle bin.

[link http://msdn.microsoft.com/en-us/library/bb762160%28VS.85%29.aspx]SHEmptyRecycleBin[/link]

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

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

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.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top