Make your own free website on Tripod.com


Recycle Bin in Windows part 1

This 2 part API tip will show you how to send files to, clear, and view Recycle Bin. In this article it is assumed that your system settings are set to send the deleted files to the Recycling Bin. Most of API material presented in this tip is also in API Viewer which comes with Visual Basic. One function and several constants were not in API Viewer. Please copy them for your own use. In part 1 you will learn how to delete a file (thus send it to Recycle Bin) and how to clear the contents of Recycle Bin straight from your app!

First of all, every time you delete a file in Windows, a dialog box comes up asking if you want to send file to the Recycling Bin. In this tip, do delete a file, I'll use Shell function called SHFileOperation, which is declared in API Viewer as:

Public Declare Function SHFileOperation Lib "shell32.dll" (lpFileOp As SHFILEOPSTRUCT) As Long

This function returns 0 if successful. Now you can see that this function uses SHFILEOPSTRUCT which can be also found in API Viewer. If you don't have API Viewer, here is the declaration:

Public Type SHFILEOPSTRUCT
   hwnd As Long              'handle to a window, can be 0
   wFunc As Long             'one of the 4 available constants shown later
   pFrom As String             'string indicating a path to the file that function will work on (must be zero terminated)
   pTo As String               'string indicating the resulting file after operation 
   fFlags As Integer            'shown later
   fAnyOperationsAborted As Long 
   hNameMappings As Long
   lpszProgressTitle As Long ' only used if FOF_SIMPLEPROGRESS
End Type

wFunc can be one of the following constants:

Public Const FO_COPY = &H2 'copy operation
Public Const FO_DELETE = &H3 'delete operation
Public Const FO_MOVE = &H1 'move operation
Public Const FO_RENAME = &H4 'rename operation, don't use with multiple files


fFlags can be one of the following constants:

Public Const FOF_ALLOWUNDO = &H40
Public Const FOF_CONFIRMMOUSE = &H2
Public Const FOF_FILESONLY = &H80
Public Const FOF_MULTIDESTFILES = &H1 'The pTo member specifies multiple destination files (one for each source file) rather 'than one directory where all source files are to be deposited
Public Const FOF_NOCONFIRMATION = &H10 'Respond with "Yes to All" for any dialog box that is displayed.
Public Const FOF_NOCONFIRMMKDIR = &H200 'Do not confirm the creation of a new directory if the operation requires one to be 'created.
Public Const FOF_RENAMEONCOLLISION = &H8 'Give the file being operated on a new name in a move, copy, or rename 'operation if a file with the target name already exists
Public Const FOF_SILENT = &H4 'Do not display a progress dialog box
Public Const FOF_SIMPLEPROGRESS = &H100 'Display a progress dialog box but do not show the file names.
Public Const FOF_WANTMAPPINGHANDLE = &H20

hNameMappings and FOF_WANTMAPPINGHANDLE will be used by VB programmers rarely and are not discussed here. So, copy the API information presented here, and paste it into your module. Every time I use API functions, I like to add to the project a Module where I keep my all API declarations. It's convenient that way.

To send a file to a Recycle Bin, call SHFileOperation with SHFileOPStuct wFunc member set to FO_DELETE. Here is the example of the function that will delete a file:

Public Function Recycle(sFilename As String, bSilent As Boolean) As Boolean
'recycles the file specified in sFilename
On Error GoTo er

Dim rc As Long
Dim shfo As SHFILEOPSTRUCT

With shfo
  .wFunc = FO_DELETE 'set the flag to delete the file
  .pFrom = sFilename & Chr$(0) 'zero terminate file name
  .fFlags = FOF_ALLOWUNDO + FOF_NOCONFIRMATION 'some additional flags
  'check if we want silent or with progress bar delete
  If Not bSilent Then .fFlags = .fFlags + FOF_SIMPLEPROGRESS
End With
  'perform the operation
  Recycle = (SHFileOperation(shfo) = 0)
Exit Function

er: Recycle = False
End Function

Pretty straightforward operation: set SHFILEOPSTRUCT members to the values you want, and then call SHFileOperation function with the structure passed to it. SHFileOperation can be reused for other purposes, like moving, copying, and renaming files.

So you can delete a file, but that's not too impressive! Files can be easily deleted many other ways. Here is something impressive: emptying the contents of the Recycle Bin straight from your app! Here is the function that will do the trick:

Public Declare Function SHEmptyRecycleBin Lib "Shell32" Alias "SHEmptyRecycleBinA" (ByVal hwnd As Long, ByVal _        pszRootPath As String, ByVal dwFlags As Long) As Long

This function is not present in API Viewer! It was taken from MSDN Library. Copy the declaration and use it as you wish! hwnd parameter can be 0, pszRootPath should be a string to a drive letter containing your Recycling Bin. When I tested the app, it seemed not to metter which drive letter you pass, so just pass C: all the time. If you do notice that it matters what letter is passed, please let me know. dwFlags also were not in API Viewer and were taken from shellapi.h file.

Public Const SHERB_NOCONFIRMATION = 1
Public Const SHERB_NOPROGRESSUI = 2
Public Const SHERB_NOSOUND = 4

Using this function is relatively simple, here is the example:

Public Function EmptyRecycleBin(Optional hwnd As Long, Optional bSilent _
As Boolean, Optional sRBLoc As String) As Long
'empties the recycle bin
On Error GoTo er

Dim lFlags As Long

   'check for correct handle to window
   If IsNumeric(hwnd) = False Then hwnd = 0
  
   'see if silent or not
   If bSilent Then lFlags = SHERB_NOPROGRESSUI
  
   'empty the recycle bin
   EmptyRecycleBin = SHEmptyRecycleBin(hwnd, sRBLoc, lFlags)

   Exit Function
er:
   EmptyRecycleBin = -1
End Function

Easy! Don't you think? Next tip will show you how to open Recycle Bin from your application! It's really cool.
 

Written By Laimonas Simutis. 2002. laijerrad@yahoo.com