Visual Basic Win32 Shell Routines
SHFileOperation: Add Recycle Bin Functionality
     
Posted:   Saturday February 8, 1997
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB4-32, Windows 95
OS restrictions:   None
Author:   VBnet - Randy Birch
     

Related:  

SHFileOperation: Copy or Move an Entire Directory
SHFileOperation: Copy, Move or Delete Files
SHQueryRecycleBin: Recycle Bin Management
       
 Prerequisites
None.

Windows 95 introduced the MAC-like ability to send files to a recycle bin, providing the user with the opportunity to reclaim these files later. The Visual Basic "Kill" statement, traditionally used to delete files, does not utilize the recycle bin but rather nukes the file outright.

Thus, my effort towards the global desire to always recycle is presented here ... how to send a file to either the Recycle Bin, or to permanently delete it, using the SHFileOperation API.

 BAS Module Code
None.

 Form Code
Start a new project, and to the form add a command button (Command1), a FileList control (File1) and a DirList control (Dir1) to provide the current path.  Also add two option buttons in a control array (Option1(0) and Option1(1)).

To reduce the possibility of errantly deleting a key file while testing this demo, the Load event of this demo sets the FileList pattern property to "*.txt". Therefore before testing, create a folder for the project and into it create a series of empty text files. It is also recommended that you keep a backup of this folder during design, as an errant call can delete (not recycle) the entire folder. Learn from my mistakes.

Add the following to the form:


Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright 1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Type SHFILEOPSTRUCT
   hWnd        As Long
   wFunc       As Long
   pFrom       As String
   pTo         As String
   fFlags      As Integer
   fAborted    As Boolean
   hNameMaps   As Long
   sProgress   As String
 End Type
  
Private Const FO_DELETE As Long = &H3
Private Const FOF_ALLOWUNDO As Long = &H40

Private Declare Function SHFileOperation Lib "shell32" _
   Alias "SHFileOperationA" _
  (lpFileOp As SHFILEOPSTRUCT) As Long
    
Private Sub Form_Load()

   Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2

   Option1(0).Caption = "to the Recycle Bin"
   Option1(0).Value = True
   Option1(1).Caption = "permanently"
   Command1.Caption = "Perform Action"
   Command1.Enabled = False
   File1.Pattern = "*.txt"
  
End Sub


Private Sub Command1_Click()
 
  'working variables 
   Dim file2Delete As String
   Dim fPath As String
   Dim FOF_FLAGS As Long
 
  'get the selected file and
  'the current path, and create  
  'a qualified path to the file 
  'from this data 
   file2Delete = File1.List(File1.ListIndex)
   fPath = Dir1.Path

   file2Delete = fPath & "\" & file2Delete
 
  'Get the chosen delete option. 
  'Assume its a permanent deletion,
  'then check if Option1(0) 
  'is true; if so, set the flag
  'to FOF_ALLOWUNDO 
   FOF_FLAGS = 0&   
   If Option1(0).Value Then FOF_FLAGS = FOF_ALLOWUNDO
 
  'delete it 
   Call ShellDeleteOne(file2Delete, FOF_FLAGS)
 
   File1.Refresh
   Command1.Enabled = File1.ListIndex > -1

End Sub


Private Sub ShellDeleteOne(sFile As String, FOF_FLAGS As Long)
 
  'set some working variables 
   Dim shf As SHFILEOPSTRUCT
 
  'terminate the file string with
  'a pair of null chars
   sFile = sFile & vbNullChar & vbNullChar
 
  'set up the SHFile options 
   With shf
      .wFunc = FO_DELETE  'action to perform
      .pFrom = sFile      'the file to act on
      .fFlags = FOF_FLAGS 'special flags
   End With
 
  'perform the delete 
   Call SHFileOperation(shf)

End Sub


Private Sub Dir1_Change()

   File1.Path = Dir1.Path
   
End Sub


Private Sub File1_Click()

   Command1.Enabled = File1.ListIndex > -1
  
End Sub
 Comments
Run the project (after saving and copying the folder to another location), and select a file. Select a delete option, and hit perform. Depending on the option chosen, the appropriate system confirmation messagebox will appear. By OR'ing the flag FOF_NOCONFIRMATION (&H10) with FOF_ALLOWUNDO, the deletion prompt dialog is suppressed.

 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright 1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter