I know this is an old post, but as I recently set out to accomplish this same task, I thought I would share the result. Using various peoples input I was able to create a simple, self contained VBScript function that will delete a file to the recycle bin
without displaying any confirmation dialog. The function has clear comments, so it should be easy to follow what is happening.
I have to say, I find it pretty disappointing how many people on this thread, and others, said it could not be done, or would require a 3rd party tool of some kind.
EXAMPLE USAGE
tMyResponse = vFn_File_Recycle ("C:\test1.txt")
MsgBox tMyResponse
FUNCTION CODE
Function vFn_File_Recycle (vFilePath)
'Sends the file 'vFilePath' to the recycle bin without any delete confirmation. Returns 0 for no
'error, else returns the Err.Number.
'---------------------------------------------------------------------------------------------------
On Error Resume Next
'VERIFY FILE EXISTS
Set f_ObjFSO = CreateObject("Scripting.FileSystemObject")
If Not f_ObjFSO.FileExists(vFilePath) Then vExists = 0 Else vExists = 1
'CONTINUE IF FILE EXISTS
If vExists = 1 Then
'BACKUP CURRENT USER RECYCLE BIN SETTINGS
Set f_ObjReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
f_ObjReg.GetBinaryValue &H80000001, "Software\Microsoft\Windows\CurrentVersion\Explorer", "ShellState", vStateArr
vBackupState = vStateArr
vStateArr(4) = 39
f_ObjReg.SetBinaryValue &H80000001, "Software\Microsoft\Windows\CurrentVersion\Explorer", "ShellState", vStateArr
'INVOTE SHELL APPLICATION DELETE COMMAND
Set f_ObjShell = CreateObject("Shell.Application").NameSpace(0).ParseName(vFilePath).InvokeVerb("delete")
'RESTORE USER RECYCLE BIN SETTINGS
f_ObjReg.SetBinaryValue &H80000001, "Software\Microsoft\Windows\CurrentVersion\Explorer", "ShellState", vBackupState
End If
'CHECK FOR ERRORS AND CLOSE THE FUNCTION
If Err.Number <> 0 Then vFn_File_Recycle = 0 Else vFn_File_Recycle = Err.Number
On Error Goto 0
End Function
-
Edited by
JoCoTechSupport
Monday, March 16, 2015 10:20 PM
Typo