Click here to Skip to main content
Click here to Skip to main content

DiskCleaner: Useless files eraser in VBScript

, 18 Aug 2005 CPOL
Rate this:
Please Sign up or sign in to vote.
A tutorial script showing how to get the paths correctly in VBScript and use them to erase useless files.

Introduction

Windows Scripting Host and VBScript offer quite a lot of ways to get the so-called special folders (Windows, My Documents, Application Data, Temp directory, ...). This script presents them all and shows an application to clean the disk by removing what I consider useless files. You should read and understand what it will delete on your disk before launching the script or your disk will be as clean as mine, maybe a bit too clean for you!

Consider it as a utility to quickly clean up your disk or as a tutorial to finally know how to get these special folder names without having to hard code them.

What does it do?

Diskcleaner [/v] [/q] [/r] deletes:

  • files: the user's recent files, the system temp folder, the user temp folder; Office XP recent files, Temporary internet files.
  • registry keys: Media Player's recent files, Explorer's recent files.

Options are:

  • no command line parameters: display a message when a file cannot be deleted because it is locked.
  • /v: verbose, display messages for every action.
  • /q: quiet, does not display the default messages. Supersedes /v.
  • /r: ask for confirmation before changing the registry.

You may change the script before using it if you do not want to erase some stuff, such as the recent files for example.

Code

First, declare the usual objects, mainly a Wscript.Shell and the FileSystemObject:

' Declare basic objects
dim wshFSO :    Set wshFSO = CreateObject("Scripting.FileSystemObject")
dim wshShell :  Set wshShell = CreateObject("WScript.Shell")
dim wshSysEnv : Set wshSysEnv = wshShell.Environment
dim wshUsrEnv : Set wshUsrEnv = wshShell.Environment("User")
dim wshPrcEnv : Set wshPrcEnv = wshShell.Environment("Process")
dim wshNet :    Set wshNet = WScript.CreateObject("WScript.Network")
dim wshArgs :   Set wshArgs = WScript.Arguments

Read the arguments. For instance, to find /v: blnVerbose gets true if verbose is required.

dim I
dim blnVerbose : blnVerbose = False
For I = 0 to wshArgs.Count - 1
    blnVerbose = blnVerbose Or (wshArgs(I)="/v") Or (wshArgs(I)="/V")
Next

The array arrFolderList is then filled with the paths of the folders to delete. They are a good sample of the different ways to get paths (see below).

To clean the folders, a classical recursive procedure is used to erase files one by one: some may be locked and generate an error which must be cleared.

Finally a temp file is created containing the registry changes to do. Regedit is called to apply it (more robust than using VBScript registry procedures).

How to locate special folder

I tried to gather all possibilities to do that. The simplest ways are listed first.

wshShell.SpecialFolders("Name")

Use the SpecialFolders property of a WScript.Shell object. This will display the path for My Documents:

dim wshShell :  Set wshShell = Createobject("WScript.Shell")
Msgbox wshShell.SpecialFolders("MyDocuments")

Available folders are (names are self sufficient):

  • AllUsersDesktop
  • AllUsersStartMenu
  • AllUsersPrograms
  • AllUsersStartup
  • Desktop
  • Favorites
  • Fonts
  • MyDocuments
  • NetHood
  • PrintHood
  • Programs
  • Recent
  • SendTo
  • StartMenu
  • Startup
  • Templates

wshFSO.GetSpecialFolder(Value)

The FileSystemObject has a GetSpecialFolder method that is able to return the paths for Windows and the temp folders. This will show the Windows folder:

dim wshFSO :    Set wshFSO = CreateObject("Scripting.FileSystemObject")
Msgbox wshFSO.GetSpecialFolder(0)

Available values are (declare the constants):

Const WindowsFolder   = 0
Const  SystemFolder   = 1
Const TemporaryFolder = 2

Environment paths

Environment paths are returned by the environment property of the shell. Variables may be system, user or process based. An array should be declared for each (the first one is "System"):

dim wshSysEnv : Set wshSysEnv = wshShell.Environment
dim wshUsrEnv : Set wshUsrEnv = wshShell.Environment("User")
dim wshPrcEnv : Set wshPrcEnv = wshShell.Environment("Process")

This will show the user's temp folder:

Msgbox wshUsrEnv("TEMP")

Available values are in the comments of the script. Actually, this method is really useful only to get the system temp directory (usually C:\Windows\Temp). The system TEMP variable is used. It will usually return "%SystemRoot%\Temp" so it must be processed to get a hard path:

dim strSysTempFolder : strSysTempFolder = wshSysEnv("TEMP")
' Develop %SystemRoot%
strSysTempFolder = Replace(strSysTempFolder, "%SystemRoot%", _
               wshPrcEnv("SYSTEMROOT"), 1, -1, vbTextCompare)

Quite complicated but I do not know another way.

Direct use of Windows environment

Not all variables returned when you type "Set" at the command prompt are available in the previous tables. To get them all, use the ExpandEnvironmentStrings method:

WshShell.ExpandEnvironmentStrings("%UserProfile%")

The variable name is typed between %. Only the process environment is available.

Except for a special-purpose path entered as an environment variable by a program, this method is of little use.

Registry

Last but not least, the registry stores paths used by Explorer and most Microsoft applications in the HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\key. This will show the Temporary internet files folder:

Msgbox WshShell.RegRead("HKEY_CURRENT_USER\Software\" & _ 
   "Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Cache")

The complete list of keys for Windows XP is in the script comments.

Which method to use

Use the solutions in the order they are listed here. Rather use SpecialFolder("MyDocuments") than the registry key. The built-in methods and properties (SpecialFolders and GetSpecialFolder) are generally powerful enough but environment variables are mandatory to get the Windows temp folder's path and the registry to get the temporary internet files.

History

Version 1.1

Features:

  • /v changed, /q and /r added to allow the user see what the script is doing.

Bug corrections:

  • The script did not wait for the user to confirm modifying the registry and deleted the temp file before it was processed, so /v did not allow changing the registry.

Version 1.0

First release.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Eric Marcon
Engineer
France France
No Biography provided

Comments and Discussions

 
GeneralExpanding user or system variable Pinmemberrcarls153-Jul-07 4:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 19 Aug 2005
Article Copyright 2004 by Eric Marcon
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid