Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for recycle.vbs

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

  1. Option Explicit
  2.  
  3. Dim colItems, objDesktop, objDtFldr, objFolder
  4. Dim objFSO, objMyDocsFolder, objMyDocuments
  5. Dim objProgFiles, objProgFolder
  6. Dim objShell, objWinDir, objWindows
  7. Dim i, strFolder, strProf
  8.  
  9. Const MY_DOCUMENTS  = &H5
  10. Const DESKTOP       = &H10
  11. Const WINDOWS       = &H24
  12. Const PROGRAM_FILES = &H26
  13.  
  14. ' Parse command line
  15. With WScript.Arguments
  16. 	If .Unnamed.Count <> 1 Then Syntax
  17. 	If .Named.Count    > 0 Then Syntax
  18. 	strFolder = Replace( .Unnamed(0), """", "" )
  19. End With
  20.  
  21. ' Open the Shell Folders object
  22. Set objShell  = CreateObject( "Shell.Application" )
  23.  
  24. ' Check if a valid folder was specified
  25. Set objFSO = CreateObject( "Scripting.FileSystemObject" )
  26. ' Folder should at least exist
  27. If Not objFSO.FolderExists( strFolder )         Then Syntax
  28. ' Skip root directories
  29. If Len( strFolder )                        <  4 Then Syntax
  30. If objFSO.GetParentFolderName( strFolder ) = "" Then Syntax
  31. ' Skip the Windows directory
  32. Set objWindows = objShell.Namespace( WINDOWS )
  33. Set objWinDir  = objWindows.Self
  34. If InStr( 1, strFolder, objWinDir.Path, vbTextCompare ) Then Syntax
  35. Set objWinDir  = Nothing
  36. Set objWindows = Nothing
  37. ' Skip Program Files folder
  38. Set objProgFiles  = objShell.Namespace( PROGRAM_FILES )
  39. Set objProgFolder = objProgFiles.Self
  40. If InStr( 1, strFolder, objProgFolder.Path, vbTextCompare ) Then Syntax
  41. Set objProgFolder = Nothing
  42. Set objProgFolder = Nothing
  43. ' Skip My Documents
  44. Set objMyDocuments  = objShell.Namespace( MY_DOCUMENTS )
  45. Set objMyDocsFolder = objMyDocuments.Self
  46. If InStr( 1, strFolder, objMyDocsFolder.Path, vbTextCompare ) Then Syntax
  47. Set objMyDocsFolder = Nothing
  48. Set objMyDocuments  = Nothing
  49. ' Skip profiles
  50. Set objDesktop = objShell.Namespace( DESKTOP )
  51. Set objDtFldr  = objDesktop.Self
  52. strProf = objFSO.GetParentFolderName( objFSO.GetParentFolderName( objDtFldr.Path ) )
  53. If InStr( 1, strFolder, strProf, vbTextCompare ) Then Syntax
  54. Set objDtFldr  = Nothing
  55. Set objDesktop = Nothing
  56. Set objFSO     = Nothing
  57.  
  58. ' Create an object for the specified folder
  59. Set objFolder = objShell.Namespace( strFolder )
  60. Set colItems  = objFolder.Items
  61.  
  62. ' Delete everything in the specified folder
  63. If colItems.Count > 0 Then
  64.     colItems.InvokeVerbEx( "Delete" )
  65. End If
  66.  
  67.  
  68. Sub Syntax
  69. 	Dim strMsg
  70. 	strMsg = "Recycle.vbs,  Version 1.10" _
  71. 	       & vbCrLf _
  72. 	       & "Move the contents of a folder to the Recycle Bin"                 _
  73. 	       & vbCrLf & vbCrLf _
  74. 	       & "Usage:  " & UCase( WScript.ScriptName ) & "  folder_path" _
  75. 	       & vbCrLf & vbCrLf _
  76. 	       & "Where:  folder_path  specifies the folder to be purged"           _
  77. 	       & vbCrLf & vbCrLf _
  78. 	       & "Note:   Deleting root directories, profile folders, the Program Files or"    _
  79. 	       & vbCrLf _
  80. 	       & "        Windows folder or My Documents is not allowed." _
  81. 	       & vbCrLf & vbCrLf _
  82. 	       & "Written by Rob van der Woude" _
  83. 	       & vbCrLf _
  84. 	       & "http://www.robvanderwoude.com"
  85. 	WScript.Echo strMsg
  86. 	WScript.Quit 1
  87. End Sub
  88.  

page last uploaded: 2017-07-06, 12:37