Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for deltrash.vbs

(view source code of deltrash.vbs as plain text)

  1. Option Explicit
  2.  
  3. On Error Resume Next
  4.  
  5. Const RECYCLE_BIN = &Ha&
  6.  
  7. Dim arrFiles, arrFolders, colItems, objRecycler
  8. Dim objItem, objFSO, objShell, strMsg, strRecycler
  9.  
  10. strMsg = ""
  11.  
  12. ' Check command line arguments: only /Q or /H are allowed
  13. If WScript.Arguments.Unnamed.Count > 0 Then Syntax
  14. Select Case WScript.Arguments.Named.Count
  15. 	Case 0
  16. 		' This is ok. just empty the recycle bin
  17. 	Case 1
  18. 		' This is ok only if the argument is either /H or /Q
  19. 		If Not ( WScript.Arguments.Named.Exists( "H" ) Or _
  20. 		         WScript.Arguments.Named.Exists( "Q" ) ) Then Syntax
  21. 	Case 2
  22. 		' This should mean both /H and /Q
  23. 		If Not ( WScript.Arguments.Named.Exists( "H" ) And _
  24. 		         WScript.Arguments.Named.Exists( "Q" ) ) Then Syntax
  25. 	Case Else
  26. 		Syntax
  27. End Select
  28.  
  29. ' Get file system object
  30. Set objFSO      = CreateObject( "Scripting.FileSystemObject" )
  31. ' Get Recycler object
  32. Set objShell    = CreateObject( "Shell.Application" )
  33. Set objRecycler = objShell.Namespace( RECYCLE_BIN )
  34. Set colItems    = objRecycler.Items
  35. ' ArrayLists to contain file and folder names
  36. Set arrFiles    = CreateObject( "System.Collections.ArrayList" )
  37. Set arrFolders  = CreateObject( "System.Collections.ArrayList" )
  38.  
  39. ' Add each item either to the files list or the folders list
  40. For Each objItem in colItems
  41. 	If objItem.IsFileSystem And Not objItem.IsLink Then
  42. 		strRecycler = objFSO.GetParentFolderName( objItem.Path )
  43. 		arrFolders.Add objItem.Path
  44. 	ElseIf objItem.IsFolder Then
  45. 		strRecycler = objFSO.GetParentFolderName( objItem.Path )
  46. 		arrFolders.Add objItem.Path
  47. 	Else
  48. 		strRecycler = objFSO.GetParentFolderName( objItem.Path )
  49. 		arrFiles.Add objItem.Path
  50. 	End If
  51. Next
  52.  
  53. ' Recurse the folders we have so far
  54. For Each objItem In arrFolders
  55. 	ListFolder objItem
  56. Next
  57.  
  58. ' Delete all files
  59. For Each objItem In arrFiles
  60. 	objFSO.DeleteFile objItem, True
  61. Next
  62.  
  63. ' Delete all folders
  64. For Each objItem In arrFolders
  65. 	objFSO.DeleteFolder objFSO.GetFolder( objItem ), True
  66. Next
  67.  
  68. ' Show result unless /Q switch was used
  69. If Not WScript.Arguments.Named.Exists( "Q" ) Then
  70. 	strMsg = "Deleted " & arrFiles.Count & " files"
  71. 	If arrFolders.Count > 0 Then
  72. 		strMsg = strMsg & " and " & arrFolders.Count & " folders"
  73. 	End If
  74. 	WScript.Echo strMsg
  75. End If
  76.  
  77. ' If /H argument was given, delete the "history" file too
  78. If WScript.Arguments.Named.Exists( "H" ) Then
  79. 	If IsObject( colItems ) Then
  80. 		objFSO.DeleteFile strRecycler & "\INFO2", True
  81. 	End If
  82. End If
  83.  
  84. ' Release the objects
  85. Set arrFiles    = Nothing
  86. Set arrFolders  = Nothing
  87. Set colItems    = Nothing
  88. Set objRecycler = Nothing
  89. Set objShell    = Nothing
  90. Set objFSO      = Nothing
  91.  
  92.  
  93. ' Recursively list files and folders
  94. Sub ListFolder( byVal myFolder )
  95. 	Dim objFile, objFolder, objSubFolder
  96. 	arrFolders.Add myFolder
  97. 	Set objFolder = objFSO.GetFolder( myFolder )
  98. 	' List files
  99. 	For Each objFile In objFolder.Files
  100. 		arrFiles.Add objFile.Path
  101. 	Next
  102. 	' List subfolders
  103. 	For Each objSubFolder In objFolder.SubFolders
  104. 		ListFolder objSubFolder.Path
  105. 	Next
  106. End Sub
  107.  
  108.  
  109. Sub Syntax
  110. 	strMsg = vbCrLf _
  111. 	       & "DelTrash.vbs,  Version 0.50 beta" & vbCrLf _
  112. 	       & "Empty Windows' recycle or trash bin without asking for confirmation" & vbCrLf & vbCrLf _
  113. 	       & "Usage:   DELTRASH.VBS  [ /H ]  [ /Q ]" & vbCrLf & vbCrLf _
  114. 	       & "Where:   /H  deletes the ""history"" file INFO2 as well (see warning)" & vbCrLf _
  115. 	       & "         /Q  suppresses screen output (number of deleted files and folders)" & vbCrLf & vbCrLf _
  116. 	       & "Based on an article by the Scripting Guys:" & vbCrLf _
  117. 	       & "http://www.microsoft.com/technet/scriptcenter/resources/qanda/may06/hey0501.mspx" & vbCrLf & vbCrLf _
  118. 	       & "Warning: This script has been tested on my own PC only." & vbCrLf _
  119. 	       & "         When the ""history"" file INFO2 is deleted (/H switch), the recycler" & vbCrLf _
  120. 	       & "         is no longer capable of restoring deleted files until it is emptied" & vbCrLf _
  121. 	       & "         manually once or twice. So you risk losing data if you forget this!" & vbCrLf _
  122. 	       & "         Use this script entirely at your own risk!" & vbCrLf & vbCrLf _
  123. 	       & "Written by Rob van der Woude" & vbCrLf _
  124. 	       & "http://www.robvanderwoude.com"
  125. 	WScript.Echo strMsg
  126. 	WScript.Quit 1
  127. End Sub
  128.  

page last modified: 2024-04-16; loaded in 0.0072 seconds