Summary:
Procedure sends a file to the Recycle Bin and updates the Recycle Bin icon.
VB Code:
Option Explicit
'declare api procedures
Private Declare Function SHFileOperation Lib "shell32.dll" _
Alias "SHFileOperationA" _
(lpFileOp As SHFILEOPSTRUCT) As Long
Private Declare Function SHUpdateRecycleBinIcon Lib "shell32.dll" () As Long
'declare constants
Private Const FO_DELETE = &H3
Private Const FOF_ALLOWUNDO = &H40
'declare udt's
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
Public Sub RecycleFile(FileSpec As String)
On Error Resume Next
'initialize variable
Dim SHFileOp As SHFILEOPSTRUCT
'configure file operation
With SHFileOp
'...specify `Delete` operation
.wFunc = FO_DELETE
'...specify the file
.pFrom = FileSpec
'...specify send to recycle bin
.fFlags = FOF_ALLOWUNDO
End With
'perform file operation
SHFileOperation SHFileOp
'update recycle bin icon
SHUpdateRecycleBinIcon
End Sub
Usage Example:
VB Code:
RecycleFile "c:\recycleme.txt"