Delete files to Recycle Bin via VBS and/or WMI

Hello,

I'd like to create a script that deletes a file into the Recycle Bin (without the confirmation dialog). I wonder if that's possible through WMI or VBS. I couldn't find anything on the net. I'm thinking in VBS (in contrast to PowerShell) to be compatible with arbitrary XP+ machines.

Any suggestion would be much appreciated.

Thanks,
Levente.

March 14th, 2012 11:29pm

Have you tried batch files? 

del /?

del *.txt /S /Q

Free Windows Admin Tool Kit Click here and download it now
March 14th, 2012 11:40pm

Hello,

I'd like to create a script that deletes a file into the Recycle Bin (without the confirmation dialog). I wonder if that's possible through WMI or VBS. I couldn't find anything on the net. I'm thinking in VBS (in contrast to PowerShell) to be compatible with arbitrary XP+ machines.

Any suggestion would be much appreciated.

Thanks,
Levente.

The recycle bin is an arm of Explorer.  Scripts and batch files cannot delete to the recycle bin.

I am not suere but Shell.Application might be able to do this as it is also an arm of Explorer.

March 15th, 2012 12:05am

Yes - shell.application FolderItem.InvofkeVerb 'delete' will delete to the recycle bin.

http://msdn.microsoft.com/en-us/library/windows/desktop/bb787810(v=vs.85).aspx

The MSDN site has numerous vbscript examples of how to use the shell.application object.

Free Windows Admin Tool Kit Click here and download it now
March 15th, 2012 12:13am

Hello,

I'd like to create a script that deletes a file into the Recycle Bin (without the confirmation dialog). I wonder if that's possible through WMI or VBS. I couldn't find anything on the net. I'm thinking in VBS (in contrast to PowerShell) to be compatible with arbitrary XP+ machines.

Any suggestion would be much appreciated.

Thanks,
Levente.

The recycle bin is an arm of Explorer.  Scripts and batch files cannot delete to the recycle bin.

I am not suere but Shell.Application might be able to do this as it is also an arm of Explorer.

March 15th, 2012 12:34am

Hi,

As was noted, the recycle bin is a feature of the Windows Explorer shell and you can invoke the "delete" verb. However, this is not robust as the verb required is language-specific.

Bill

Free Windows Admin Tool Kit Click here and download it now
March 15th, 2012 3:07am

Hi,

As was noted, the recycle bin is a feature of the Windows Explorer shell and you can invoke the "delete" verb. However, this is not robust as the verb required is language-specific.

Bill

Bill - yes but...

There is no other way to do this via scripting.  We can use the API from a compiled program but not from script.

March 15th, 2012 3:36am

Correct, that is the only way via native scripting. This is why I don't recommend it -- because it is language-specific.

Bill

Free Windows Admin Tool Kit Click here and download it now
March 15th, 2012 8:19pm

abqBill:

Robust is great, so if I were put in the position where I didn't know my script's "audience", I'd probably setup an external (community serverbased) lookup table for the appropriate verb for a likely subset of localizations settings, and if the setting returned from a localization query doesn't match any key entries in the table, I'd be half-tempted to prompt for the proper verb, and add the location key and proper verb to said table so it can grow in situ.

Either that, or perhaps there is some means of drilling down into a particular context menu and pulling the members out for comparison?

Might not be  the pinnacle of code writing, but I loathe "can't", it immediately puts me in "oh yeah?, wanna bet?" mode...

Paraphrased, I see the box as a place to think both inside and outside.  :)

DAS

March 16th, 2012 2:12pm

You're welcome to do that, of course, if you think it's worth the engineering effort.

I'd rather write an external executable that performs the appropriate API call and call that from my script.

To each his own, I guess :)

Bill

Free Windows Admin Tool Kit Click here and download it now
March 16th, 2012 2:24pm

I tend to take the leap of faith that if someone is looking for scripting help either they don't have the inclination, the means, or the knowledge to actually write an exe.

I could be wrong, its happened numerous times> :)

DAS

March 16th, 2012 2:42pm

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



Free Windows Admin Tool Kit Click here and download it now
March 16th, 2015 6:21pm

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



March 16th, 2015 10:19pm

As already noted, this solution is language-specific and thus is not a good generalized solution.

Free Windows Admin Tool Kit Click here and download it now
March 17th, 2015 10:18am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics