(view source code of deltrash.vbs as plain text)
Option Explicit
On Error Resume Next
Const RECYCLE_BIN = &Ha&
Dim arrFiles, arrFolders, colItems, objRecycler
Dim objItem, objFSO, objShell, strMsg, strRecycler
strMsg = ""
' Check command line arguments: only /Q or /H are allowed
If WScript.Arguments.Unnamed.Count > 0 Then Syntax
Select Case WScript.Arguments.Named.Count
Case 0
' This is ok. just empty the recycle bin
Case 1
' This is ok only if the argument is either /H or /Q
If Not ( WScript.Arguments.Named.Exists( "H" ) Or _
WScript.Arguments.Named.Exists( "Q" ) ) Then Syntax
Case 2
' This should mean both /H and /Q
If Not ( WScript.Arguments.Named.Exists( "H" ) And _
WScript.Arguments.Named.Exists( "Q" ) ) Then Syntax
Case Else
Syntax
End Select
' Get file system object
Set objFSO = CreateObject( "Scripting.FileSystemObject" )
' Get Recycler object
Set objShell = CreateObject( "Shell.Application" )
Set objRecycler = objShell.Namespace( RECYCLE_BIN )
Set colItems = objRecycler.Items
' ArrayLists to contain file and folder names
Set arrFiles = CreateObject( "System.Collections.ArrayList" )
Set arrFolders = CreateObject( "System.Collections.ArrayList" )
' Add each item either to the files list or the folders list
For Each objItem in colItems
If objItem.IsFileSystem And Not objItem.IsLink Then
strRecycler = objFSO.GetParentFolderName( objItem.Path )
arrFolders.Add objItem.Path
ElseIf objItem.IsFolder Then
strRecycler = objFSO.GetParentFolderName( objItem.Path )
arrFolders.Add objItem.Path
Else
strRecycler = objFSO.GetParentFolderName( objItem.Path )
arrFiles.Add objItem.Path
End If
Next
' Recurse the folders we have so far
For Each objItem In arrFolders
ListFolder objItem
Next
' Delete all files
For Each objItem In arrFiles
objFSO.DeleteFile objItem, True
Next
' Delete all folders
For Each objItem In arrFolders
objFSO.DeleteFolder objFSO.GetFolder( objItem ), True
Next
' Show result unless /Q switch was used
If Not WScript.Arguments.Named.Exists( "Q" ) Then
strMsg = "Deleted " & arrFiles.Count & " files"
If arrFolders.Count > 0 Then
strMsg = strMsg & " and " & arrFolders.Count & " folders"
End If
WScript.Echo strMsg
End If
' If /H argument was given, delete the "history" file too
If WScript.Arguments.Named.Exists( "H" ) Then
If IsObject( colItems ) Then
objFSO.DeleteFile strRecycler & "\INFO2", True
End If
End If
' Release the objects
Set arrFiles = Nothing
Set arrFolders = Nothing
Set colItems = Nothing
Set objRecycler = Nothing
Set objShell = Nothing
Set objFSO = Nothing
' Recursively list files and folders
Sub ListFolder( byVal myFolder )
Dim objFile, objFolder, objSubFolder
arrFolders.Add myFolder
Set objFolder = objFSO.GetFolder( myFolder )
' List files
For Each objFile In objFolder.Files
arrFiles.Add objFile.Path
Next
' List subfolders
For Each objSubFolder In objFolder.SubFolders
ListFolder objSubFolder.Path
Next
End Sub
Sub Syntax
strMsg = vbCrLf _
& "DelTrash.vbs, Version 0.50 beta" & vbCrLf _
& "Empty Windows' recycle or trash bin without asking for confirmation" & vbCrLf & vbCrLf _
& "Usage: DELTRASH.VBS [ /H ] [ /Q ]" & vbCrLf & vbCrLf _
& "Where: /H deletes the ""history"" file INFO2 as well (see warning)" & vbCrLf _
& " /Q suppresses screen output (number of deleted files and folders)" & vbCrLf & vbCrLf _
& "Based on an article by the Scripting Guys:" & vbCrLf _
& "http://www.microsoft.com/technet/scriptcenter/resources/qanda/may06/hey0501.mspx" & vbCrLf & vbCrLf _
& "Warning: This script has been tested on my own PC only." & vbCrLf _
& " When the ""history"" file INFO2 is deleted (/H switch), the recycler" & vbCrLf _
& " is no longer capable of restoring deleted files until it is emptied" & vbCrLf _
& " manually once or twice. So you risk losing data if you forget this!" & vbCrLf _
& " Use this script entirely at your own risk!" & vbCrLf & vbCrLf _
& "Written by Rob van der Woude" & vbCrLf _
& "http://www.robvanderwoude.com"
WScript.Echo strMsg
WScript.Quit 1
End Sub
page last modified: 2024-04-16; loaded in 0.0072 seconds