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)
- Set oWSH = CreateObject ("WScript.Shell")
- Set oFSO = CreateObject ("Scripting.FileSystemObject")
- Function ScriptPath
- ScriptPath = oFSO.GetParentFolderName(WScript.ScriptFullName) & "\"
- End Function
- Sub Sleep(x)
- Start = CInt (Second (time))
- i = 0
- Do
- i = i + 1
- Loop While CInt (Second (time)) < Start + CInt (x)
- End Sub
- Set fEBin = oFSO.CreateTextFile(ScriptPath & "Tmp_EBin.vbs",2)
- fEBin.writeline "Set oFSO = CreateObject (""Scripting.FileSystemObject"")"
- fEBin.writeline "Set oShell = CreateObject (""Shell.Application"")"
- fEBin.writeline "Set oFolder = oFSO.GetFolder(""C:\RECYCLER"")"
- fEBin.writeline "If (Not oFolder Is Nothing ) Then "
- fEBin.writeline " For Each oFldr In oFolder.SubFolders"
- fEBin.writeline " Set oNS = oShell.NameSpace(oFldr.Path)"
- fEBin.writeline " If (Not oFolder Is Nothing ) Then "
- fEBin.writeline " oNS.Self.InvokeVerb(""Empty Recycle &Bin"")"
- fEBin.writeline " Set oNS = Nothing "
- fEBin.writeline " Exit For "
- fEBin.writeline " End If "
- fEBin.writeline " Next "
- fEBin.writeline " Set oFolder = Nothing "
- fEBin.writeline "End If "
- fEBin.writeline "Set oFSO = Nothing "
- fEBin.writeline "Set oShell = Nothing "
- fEBin.close
- iReturn = oWSH.Run ("""" & ScriptPath & "Tmp_EBin.vbs""", 0, False )
- Sleep 2
-
- iReturn = oWSH.AppActivate("Confirm Multiple File Delete")
-
- If iReturn = "False " Then iReturn = oWSH.AppActivate("Confirm File Delete")
- If iReturn = "True " Then oWSH.SendKeys("{ENTER}")
-
- oFSO.DeleteFile ScriptPath & "Tmp_EBin.vbs"
- Set oWSH = Nothing
- Set oFSO = Nothing
Download Empty Recycle Bin (vbs file)