none
Delete files to Recycle Bin via VBS and/or WMI

    Question

  • 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.

    Wednesday, March 14, 2012 11:29 PM

Answers

All replies

  • Have you tried batch files? 

    del /?

    del *.txt /S /Q


    Rich Prescott | Infrastructure Architect, Windows Engineer and PowerShell blogger | MCITP, MCTS, MCP

    Engineering Efficiency
    @Rich_Prescott
    Windows System Administration tool
    AD User Creation tool

    Wednesday, March 14, 2012 11:40 PM
    Moderator
  • 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.


    ¯\_(ツ)_/¯

    Thursday, March 15, 2012 12:05 AM
  • 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.


    ¯\_(ツ)_/¯

    Thursday, March 15, 2012 12:13 AM
  • 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.


    ¯\_(ツ)_/¯

    Hrm, I never even thought about that.  Then again, I haven't checked my recycle bin in years.  I typically make sure I want the files gone before I delete them.  And I also rebuild my system about once a month, so it never fills up.  Ahh, the joys of unattended OS installs, scripted software installs, and a NAS.

    Rich Prescott | Infrastructure Architect, Windows Engineer and PowerShell blogger | MCITP, MCTS, MCP

    Engineering Efficiency
    @Rich_Prescott
    Windows System Administration tool
    AD User Creation tool

    Thursday, March 15, 2012 12:34 AM
    Moderator
  • 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

    Thursday, March 15, 2012 3:07 AM
    Moderator
  • 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.


    ¯\_(ツ)_/¯

    Thursday, March 15, 2012 3:36 AM
  • Correct, that is the only way via native scripting. This is why I don't recommend it -- because it is language-specific.

    Bill

    Thursday, March 15, 2012 8:19 PM
    Moderator
  • 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

    Friday, March 16, 2012 2:12 PM
  • 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

    Friday, March 16, 2012 2:24 PM
    Moderator
  • 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

    Friday, March 16, 2012 2:42 PM
  • 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



    Monday, March 16, 2015 10:19 PM
  • As already noted, this solution is language-specific and thus is not a good generalized solution.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, March 17, 2015 2:16 PM
    Moderator