Option Explicit Dim arrProtectedRootFolders, arrProtectedSubFolders Dim blnListOnly, blnQuiet, blnProtected, blnPurgeFolder Dim intValidArgs, intRC Dim colItems, objFolder, objFSO, objParentFolder, objProtectedFolder, objProtFolder, objShell, wshShell Dim strFolder, strProtectedFolderNameSpace, strParentFolder, strTempFolder ' Matt Wilkie's list of shell special folder constants, these folders must NEVER be removed ' https://gist.github.com/maphew/47e67b6a99e240f01aced8b6b5678eeb Const ALTSTARTUP = 29 ' File system directory that corresponds to the user's non-localized Startup program group. Const APPDATA = 26 ' Version 4.71. File system directory that serves as a common repository for application-specific data. A typical path is C:\Documents and Settings\username\Application Data. Const BITBUCKET = 10 ' Virtual folder that contains the objects in the user's Recycle Bin. Const COMMONALTSTARTUP = 30 ' File system directory that corresponds to the non-localized Startup program group for all users. Valid only for Windows NT systems. Const COMMONAPPDATA = 35 ' Version 5.0. Application data for all users. A typical path is C:\Documents and Settings\All Users\Application Data. Const COMMONDESKTOPDIR = 25 ' File system directory that contains files and folders that appear on the desktop for all users. A typical path is C:\Documents and Settings\All Users\Desktop. Valid only for Windows NT systems. Const COMMONFAVORITES = 31 ' File system directory that serves as a common repository for the favorite URLs shared by all users. Valid only for Windows NT systems. Const COMMONPROGRAMS = 23 ' File system directory that contains the directories for the common program groups that appear on the Start menu for all users. A typical path is C:\Documents and Settings\All Users\Start Menu\Programs. Valid only for Windows NT systems. Const COMMONSTARTMENU = 22 ' File system directory that contains the programs and folders that appear on the Start menu for all users. A typical path is C:\Documents and Settings\All Users\Start Menu. Valid only for Windows NT systems. Const COMMONSTARTUP = 24 ' File system directory that contains the programs that appear in the Startup folder for all users. A typical path is C:\Documents and Settings\All Users\Microsoft\Windows\Start Menu\Programs\StartUp. Valid only for Windows NT systems. Const CONTROLS = 3 ' Virtual folder that contains icons for the Control Panel applications. Const COOKIES = 33 ' File system directory that serves as a common repository for Internet cookies. A typical path is C:\Documents and Settings\username\Application Data\Microsoft\Windows\Cookies. Const DESKTOP = 0 ' Windows desktop: the virtual folder that is the root of the namespace. Const DESKTOPDIRECTORY = 16 ' File system directory used to physically store the file objects that are displayed on the desktop. It is not to be confused with the desktop folder itself, which is a virtual folder. A typical path is C:\Documents and Settings\username\Desktop. Const DRIVES = 17 ' My Computer: the virtual folder that contains everything on the local computer: storage devices, printers, and Control Panel. This folder can also contain mapped network drives. Const FAVORITES = 6 ' File system directory that serves as a common repository for the user's favorite URLs. A typical path is C:\Documents and Settings\username\Favorites. Const FONTS = 20 ' Virtual folder that contains installed fonts. A typical path is C:\Windows\Fonts. Const HISTORY = 34 ' File system directory that serves as a common repository for Internet history items. Const INTERNETCACHE = 32 ' File system directory that serves as a common repository for temporary Internet files. A typical path is C:\Users\username\AppData\Local\Microsoft\Windows\Temporary Internet Files. Const LOCALAPPDATA = 28 ' Version 5.0. File system directory that serves as a data repository for local (non-roaming) applications. A typical path is C:\Users\username\AppData\Local. Const MYPICTURES = 39 ' My Pictures folder. A typical path is C:\Users\username\Pictures. Const NETHOOD = 19 ' A file system folder that contains any link objects in the My Network Places virtual folder. It is not the same as ssfNETWORK, which represents the network namespace root. A typical path is C:\Users\username\AppData\Roaming\Microsoft\Windows\Network Shortcuts. Const NETWORK = 18 ' Network Neighborhood: the virtual folder that represents the root of the network namespace hierarchy. Const PERSONAL = 5 ' File system directory that serves as a common repository for a user's documents. A typical path is C:\Users\username\Documents. Const PRINTERS = 4 ' Virtual folder that contains installed printers. Const PRINTHOOD = 27 ' File system directory that contains any link objects in the Printers virtual folder. A typical path is C:\Users\username\AppData\Roaming\Microsoft\Windows\Printer Shortcuts. Const PROFILE = 40 ' Version 5.0. User's profile folder. Const PROGRAMFILES = 38 ' Version 5.0. Program Files folder. A typical path is C:\Program Files. Const PROGRAMFILESx86 = 42 ' Version 6.0. Program Files folder. A typical path is C:\Program Files, or C:\Program Files (X86) on a 64-bit computer. Const PROGRAMS = 2 ' File system directory that contains the user's program groups (which are also file system directories). A typical path is C:\Users\username\AppData\Roaming\Microsoft\Windows\Start Menu\Programs. Const RECENT = 8 ' File system directory that contains the user's most recently used documents. A typical path is C:\Users\username\AppData\Roaming\Microsoft\Windows\Recent. Const SENDTO = 9 ' File system directory that contains Send To menu items. A typical path is C:\Users\username\AppData\Roaming\Microsoft\Windows\SendTo. Const STARTMENU = 11 ' File system directory that contains Start menu items. A typical path is C:\Users\username\AppData\Roaming\Microsoft\Windows\Start Menu. Const STARTUP = 7 ' File system directory that corresponds to the user's Startup program group. The system starts these programs whenever any user first logs into their profile after a reboot. A typical path is C:\Users\username\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\StartUp. Const SYSTEM = 37 ' Version 5.0. The System folder. A typical path is C:\Windows\System32. Const SYSTEMx86 = 41 ' Version 5.0. System folder. A typical path is C:\Windows\System32, or C:\Windows\Syswow32 on a 64-bit computer. Const TEMPLATES = 21 ' File system directory that serves as a common repository for document templates. Const WINDOWS = 36 ' Version 5.0. Windows directory. This corresponds to the %windir% or %SystemRoot% environment variables. A typical path is C:\Windows. ' Parse command line intValidArgs = 0 With WScript.Arguments If .Unnamed.Count = 0 Then Syntax "" If .Unnamed.Count > 1 Then Syntax "Invalid command line argument(s)" If .Named.Exists( "?" ) Then Syntax "" blnListOnly = .Named.Exists( "L" ) blnQuiet = .Named.Exists( "Q" ) blnPurgeFolder = .Named.Exists( "P" ) If blnListOnly Then intValidArgs = intValidArgs + 1 If blnQuiet Then intValidArgs = intValidArgs + 1 If blnPurgeFolder Then intValidArgs = intValidArgs + 1 If .Named.Count <> intValidArgs Then Syntax "Invalid command line switch(es)" ' Remove doublequotes and leading or trailing whitespace from specified folder strFolder = Trim( Replace( .Unnamed(0), """", "" ) ) ' Check for wildcards If InStr( 1, strFolder, "*", vbTextCompare ) Or InStr( 1, strFolder, "?", vbTextCompare ) Then Syntax "Wildcards are not allowed" End With ' Expand environment variables in specified folder name Set wshShell = CreateObject("Wscript.Shell") strFolder = wshShell.ExpandEnvironmentStrings( strFolder ) Set wshShell = Nothing blnProtected = False ' Open the Shell Folders object Set objShell = CreateObject( "Shell.Application" ) ' Check if a valid folder was specified Set objFSO = CreateObject( "Scripting.FileSystemObject" ) ' Folder should at least exist If Not objFSO.FolderExists( strFolder ) Then Syntax "Invalid folder """ & strfolder & """" End If ' Get fully qualified path of folder strFolder = objFSO.GetAbsolutePathName( strFolder ) ' Skip root directories of disk drives If Len( strFolder ) < 4 Or objFSO.GetParentFolderName( strFolder ) = "" Then Syntax "Removal of root directories is not possible" End if ' Protect these folders, including their subfolders arrProtectedSubFolders = Array( BITBUCKET, CONTROLS, DESKTOP, FONTS, NETHOOD, NETWORK, PRINTERS, PRINTHOOD, PROGRAMFILES, PROGRAMFILESx86, SYSTEM, SYSTEMx86, WINDOWS ) For Each strProtectedFolderNameSpace In arrProtectedSubFolders Set objProtectedFolder = objShell.NameSpace( strProtectedFolderNameSpace ) Set objProtFolder = objProtectedFolder.Self If InStr( 1, strFolder, objProtFolder.Path, vbTextCompare ) Then Syntax "Removal of system folder """ & objProtFolder.Path & """ or its subfolders is prohibited" End If Set objProtFolder = Nothing Set objProtectedFolder = Nothing Next ' Protect these folders, but not their content arrProtectedRootFolders = Array( ALTSTARTUP, APPDATA, COMMONALTSTARTUP, COMMONAPPDATA, COMMONDESKTOPDIR, COMMONFAVORITES, COMMONPROGRAMS, COMMONSTARTMENU, COMMONSTARTUP, COOKIES, DESKTOPDIRECTORY, DRIVES, FAVORITES, HISTORY, INTERNETCACHE, LOCALAPPDATA, MYPICTURES, PERSONAL, PROFILE, PROGRAMS, RECENT, SENDTO, STARTMENU, STARTUP, TEMPLATES ) For Each strProtectedFolderNameSpace In arrProtectedRootFolders Set objProtectedFolder = objShell.NameSpace( strProtectedFolderNameSpace ) Set objProtFolder = objProtectedFolder.Self If LCase( strFolder ) = LCase( objProtFolder.Path ) Then If blnpurgefolder Then blnProtected = True Else Syntax "Removal of system folder """ & strFolder & """ is prohibited" End If End If Set objProtFolder = Nothing Set objProtectedFolder = Nothing Next ' Protect TEMP and TMP folders, but not their content If IsTempFolder( strFolder ) Then If blnPurgeFolder Then blnProtected = True Else Syntax "Removal of temporary folder """ & strFolder & """ is prohibited" End If End If ' Protect folders listed in the PATH variable, but not their content If IsInPATH( strFolder ) Then If blnPurgeFolder Then blnProtected = True Else Syntax "Removal of folder """ & strFolder & """ is prohibited because it is listed in the PATH variable" End If End If ' Set return code to 1 in case no directory was deleted intRC = 1 If blnPurgeFolder And blnProtected Then PurgeFolder strFolder Else DeleteFolder strFolder End If ' Clean up Set objShell = Nothing Set colItems = Nothing Set objParentFolder = Nothing Set objFSO = Nothing ' Return code tells if folder was successfully removed WScript.Quit intRC Sub DeleteFolder( strFolder ) ' Get parent folder's contents strParentFolder = objFSO.GetParentFolderName( strFolder ) Set objParentFolder = objShell.NameSpace( strParentFolder ) Set colItems = objParentFolder.Items ' Remove specified folder if found in parent folder's content If colItems.Count > 0 Then For Each objFolder In colItems If objFolder.IsFolder And objFolder.Path = strFolder Then If Not blnQuiet Then WScript.Echo "Moving folder """ & strFolder & """ to the Recycle Bin" If Not blnListOnly Then objFolder.InvokeVerb "Delete" intRC = 0 ' A folder WAS deleted, so return code is set to 0 End If Next End If If intRC = 1 And Not blnQuiet Then WScript.Echo "Nothing to delete" End Sub Function IsInPATH( strFolder ) Dim arrPATH, blnIsInPATH, i, strPATH, wshShell blnIsInPATH = False Set wshShell = WScript.CreateObject("Wscript.Shell") ' Remove doublequotes from PATH and expand environment embedded environment variables With wshShell strPATH = Replace( .ExpandEnvironmentStrings( .Environment.Item( "PATH" ) ), """", "" ) End With Set wshShell = Nothing arrPATH = Split( strPATH, ";" ) For i = 0 To UBound( arrPATH ) If UCase( strFolder ) = UCase( Trim( arrPATH(i) ) ) Then blnIsInPATH = True Exit For End If Next IsInPATH = blnIsInPATH End Function Function IsTempFolder( strFolder ) ' Find all TEMP and TMP folders for system and all users, and check the specified folder Dim blnIsTempFolder, colItems, objItem, objWMIService, strTempFolder, wshShell blnIsTempFolder = False Set wshShell = WScript.CreateObject("Wscript.Shell") Set objWMIService = GetObject( "winmgmts://./root/CIMV2" ) Set colItems = objWMIService.ExecQuery( "SELECT Name,VariableValue FROM Win32_Environment WHERE Name='TEMP' OR Name='TMP'" ) For Each objItem In colItems strTempFolder = wshShell.ExpandEnvironmentStrings( objItem.VariableValue ) If UCase( strTempFolder ) = UCase( strFolder ) Then blnIsTempFolder = True Exit For End If Next Set colItems = Nothing Set objWMIService = Nothing Set wshShell = Nothing IsTempFolder = blnIsTempFolder End Function Sub PurgeFolder( strFolder ) Dim objFolder Dim colItems ' Create an object for the specified folder Set objFolder = objShell.Namespace( strFolder ) Set colItems = objFolder.Items ' Delete everything in the specified folder If colItems.Count > 0 Then intRC = 0 ' At least one item in the folder was deleted, so return code is set to 0 If Not blnQuiet Then WScript.Echo "Moving content of folder """ & strFolder & """ to the Recycle Bin" If Not blnListOnly Then colItems.InvokeVerbEx( "Delete" ) Else If Not blnQuiet Then WScript.Echo "Nothing to delete" End If Set colItems = Nothing Set objFolder = Nothing End Sub Sub Syntax ( strMsg ) If Not Trim( strMsg ) = "" Then strMsg = vbCrLf & "ERROR: " & strMsg & vbCrLf End If strMsg = strMsg _ & vbCrLf _ & "Recycle.vbs, Version 2.01" _ & vbCrLf _ & "Move a folder and its contents to the Recycle Bin" _ & vbCrLf & vbCrLf _ & "Usage: " & UCase( WScript.ScriptName ) & " folder_path [ options ]" _ & vbCrLf & vbCrLf _ & "Where: folder_path specifies the folder to be purged" _ & vbCrLf & vbCrLf _ & "Options: /L List selected folder only, do not actually delete" _ & vbCrLf _ & " /P Purge files and subfolders in specified protected folder," _ & vbCrLf _ & " e.g. a temporary folder or root directory" _ & vbCrLf _ & " /Q Quiet mode, no screen output (except error messages)" _ & vbCrLf & vbCrLf _ & "Notes: Several essential folders are protected, and cannot be removed" _ & vbCrLf _ & " by this script. These include virtual folders (e.g. Desktop)," _ & vbCrLf _ & " system folders, temporary folders, root directories of disk" _ & vbCrLf _ & " drives, and all folders listed in the PATH variable. Except" _ & vbCrLf _ & " for virtual and Windows and Program Files folders, these" _ & vbCrLf _ & " folders can be purged (command line switch /P), or their" _ & vbCrLf _ & " subfolders can be deleted (see list of constants for details)." _ & vbCrLf _ & " Return code will be 0 if the specified folder was deleted" _ & vbCrLf _ & " (or listed with /L or purged with /P), or 1 otherwise." _ & vbCrLf & vbCrLf _ & "Credits: List of shell special folder constants by Matt Wilkie" _ & vbCrLf _ & " https://gist.github.com/maphew/47e67b6a99e240f01aced8b6b5678eeb" _ & vbCrLf & vbCrLf _ & "Written by Rob van der Woude" _ & vbCrLf _ & "https://www.robvanderwoude.com" WScript.Echo strMsg WScript.Quit 1 End Sub