This is a script written in VBScript designed for execution with Microsoft® Windows® Script Host (WSH). It empties the recycle bin automatically. Several alternate solutions are available from WSH Bazaar and Windows-Script. In the first case, a ActiveX control is used and must be registered using regsvr32. The second solution simply does not work. It demonstrates how to send a single file to the recycle bin. I think of this script as a hack being that it creates a second temporary script and uses the SendKeys. As a result, it exhibits the power of WSH when running Windows automation tasks.

Download Empty Recycle Bin (vbs file)

  1. Set oWSH = CreateObject ("WScript.Shell")
  2. Set oFSO = CreateObject ("Scripting.FileSystemObject")
  3. Function ScriptPath
  4.   ScriptPath = oFSO.GetParentFolderName(WScript.ScriptFullName) & "\"
  5. End Function
  6. Sub Sleep(x)
  7.   Start = CInt (Second (time))
  8.   i = 0
  9.   Do
  10.     i = i + 1
  11.   Loop While CInt (Second (time)) < Start + CInt (x)
  12. End Sub
  13. Set fEBin = oFSO.CreateTextFile(ScriptPath & "Tmp_EBin.vbs",2)
  14. fEBin.writeline "Set oFSO = CreateObject (""Scripting.FileSystemObject"")"
  15. fEBin.writeline "Set oShell = CreateObject (""Shell.Application"")"
  16. fEBin.writeline "Set oFolder = oFSO.GetFolder(""C:\RECYCLER"")"
  17. fEBin.writeline "If (Not oFolder Is Nothing ) Then "
  18. fEBin.writeline "   For Each oFldr In oFolder.SubFolders"
  19. fEBin.writeline " Set oNS = oShell.NameSpace(oFldr.Path)"
  20. fEBin.writeline " If (Not oFolder Is Nothing ) Then "
  21. fEBin.writeline "   oNS.Self.InvokeVerb(""Empty Recycle &Bin"")"
  22. fEBin.writeline "   Set oNS = Nothing "
  23. fEBin.writeline "   Exit For "
  24. fEBin.writeline " End If "
  25. fEBin.writeline " Next "
  26. fEBin.writeline " Set oFolder = Nothing "
  27. fEBin.writeline "End If "
  28. fEBin.writeline "Set oFSO = Nothing "
  29. fEBin.writeline "Set oShell = Nothing "
  30. fEBin.close
  31. iReturn = oWSH.Run ("""" & ScriptPath & "Tmp_EBin.vbs""", 0, False )
  32. Sleep 2
  33. 'MsgBox iReturn
  34. iReturn = oWSH.AppActivate("Confirm Multiple File Delete")
  35. 'MsgBox iReturn
  36. If iReturn = "False " Then iReturn = oWSH.AppActivate("Confirm File Delete")
  37. If iReturn = "True " Then oWSH.SendKeys("{ENTER}")
  38. '* Delete temp file
  39. oFSO.DeleteFile ScriptPath & "Tmp_EBin.vbs"
  40. Set oWSH = Nothing
  41. Set oFSO = Nothing

Download Empty Recycle Bin (vbs file)