Delete File To Recycle Bin

'Add a module to your project (In the menu choose Project -> Add Module, Then click Open)
'If the user windows 'delete to recycle bin' option is disabled, the file will deleted completely.
'Insert this code to the module :

Public Type SHFILEOPSTRUCT
hwnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Integer
fAnyOperationsAborted As Boolean
hNameMappings As Long
lpszProgressTitle As String
End Type
Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" _
(lpFileOp As SHFILEOPSTRUCT) As Long
Public Const F0_DELETE = &H3
Public Const F0F_ALLOWUNDO = &H40
Public Const F0F_CREATEPROGRESSDLG As Long = &H0

'Insert the following code to your form:

Private Sub Form_Load()
Dim MyBool As Boolean
'Replace 'c:\MyDir\MyFile.exe' with the name of the file you want to delete.
DelToRecycBin ("c:\MyDir\MyFile.exe")
End Sub

Public Function DelToRecycBin(FileName As String)
Dim FileOperation As SHFILEOPSTRUCT
Dim lReturn As Long
On Error GoTo DelToRecycBin_Err
With FileOperation
.wFunc = F0_DELETE
.pFrom = FileName
.fFlags = F0F_ALLOWUNDO + F0F_CREATEPROGRESSDLG
End With
lReturn = SHFileOperation(FileOperation)
Exit Function
DelToRecycBin_Err:
MsgBox Err.Description
End Function

Go Back