Option Explicit

Dim blnAge, blnCookies, blnDocHist, blnFavorites
Dim blnIEHist, blnQuiet, blnSafe, blnVerbose
Dim dtmLastBoot
Dim intAge, intFiles, intParams, intSize
Dim colItems, objFSO, objItem, objTempDir, objWMIService, wshShell
Dim strMsg, strQuery, strTempDir

blnAge       = False
blnCookies   = False
blnFavorites = False
blnIEHist   = False
blnQuiet     = False
blnSafe      = False
blnVerbose   = False
intAge       = -1
intFiles     =  0
intParams    =  0
intSize      =  0
strMsg       = ""

If WScript.Arguments.Unnamed.Count > 1 Then Syntax
If WScript.Arguments.Unnamed.Count = 1 Then
	If IsNumeric( WScript.Arguments.Unnamed(0) ) Then
		blnAge = True
		intAge = CInt( WScript.Arguments.Unnamed(0) )
	Else
		Syntax
	End If
End If
If WScript.Arguments.Named.Exists( "V" ) Then
	blnVerbose = True
	intParams  = intParams + 1
End If
If WScript.Arguments.Named.Exists( "S" ) Then
	If blnAge Then
		Syntax ' days (age) and /S are mutually exclusive
	Else
		blnSafe   = True
		intParams = intParams + 1
	End If
End If
If WScript.Arguments.Named.Exists( "Q" ) Then
	If blnVerbose Then
		Syntax ' /Q and /V are mutually exclusive
	Else
		blnQuiet  = True
		intParams = intParams + 1
	End If
End If
If WScript.Arguments.Named.Exists( "C" ) Then
	blnCookies = True
	intParams  = intParams + 1
End If
If WScript.Arguments.Named.Exists( "D" ) Then
	blnDocHist = True
	intParams  = intParams + 1
End If
If WScript.Arguments.Named.Exists( "F" ) Then
	blnFavorites = True
	intParams    = intParams + 1
End If
If WScript.Arguments.Named.Exists( "H" ) Then
	blnIEHist = True
	intParams = intParams + 1
End If
If intParams < WScript.Arguments.Named.Count Then Syntax

Set objFSO   = CreateObject( "Scripting.FileSystemObject" )
Set wshShell = Wscript.CreateObject( "Wscript.Shell" )

DelShellFolder "Cache"                           ' Empty (IE) Cache
If blnIEHist    Then DelShellFolder "History"    ' Empty (IE) History
If blnDocHist   Then DelShellFolder "Recent"     ' Empty Documents history
If blnCookies   Then DelShellFolder "Cookies"    ' Remove (IE) Cookies
If blnFavorites Then DelShellFolder "Favorites"  ' Remove (IE) Favorites

' Check if safe mode was requested (for TEMP directory only)
If blnSafe Then
	strQuery = "Select * from Win32_OperatingSystem"
	Set objWMIService = GetObject( "winmgmts://./root/cimv2" )
	Set colItems      = objWMIService.ExecQuery( strQuery, , 48 )
	For Each objItem In colItems
		dtmLastBoot = objItem.LastBootUpTime
	Next
	Set colItems      = Nothing
	Set objWMIService = Nothing
	blnAge      = True
	dtmLastBoot = CDate( Mid( dtmLastBoot, 7, 2 ) & " " _
	            & MonthName( Mid( dtmLastBoot, 5, 2 ) ) & " " _
	            & Left( dtmLastBoot, 4 ) )
	intAge      = DateDiff( "d", dtmLastBoot, Now )
End If

strTempDir     = wshShell.ExpandEnvironmentStrings( "%TEMP%" )
Set objTempDir = objFSO.GetFolder( strTempDir )
DelTree objTempDir, False
Set objTempDir = Nothing

If intFiles > 0 Then
	strMsg = strMsg & intFiles & " file" 
	If intFiles > 1 Then strMsg = strMsg & "s"
	strMsg = strMsg & " could not be deleted due to being in use - The rest were deleted!  (" _
	       & FormatNumber( intSize / 1048576, 1, True, False, False ) _
	       & " MB)"
End If

If Not blnQuiet Then WScript.Echo strMsg

Set objFSO   = Nothing
Set wshShell = Nothing


Sub DelShellFolder( myShellFolder )
	Dim objShellFolder, strRegKey, strShellFolder
	strRegKey      = "HKEY_CURRENT_USER\Software\Microsoft\Windows\" _
	               & "CurrentVersion\Explorer\Shell Folders\" & myShellFolder
	strShellFolder = wshShell.RegRead( strRegKey )
	Set objShellFolder = objFSO.GetFolder( strShellFolder )
	DelTree objShellFolder, False
	Set objShellFolder = Nothing
End Sub


Sub DelTree( ByRef myFolder, blnDeleteRoot )
	Dim intFileAge, intMySize, objMyFile, objMyFolder, objSubFolder, strPath
	Set objMyFolder = objFSO.GetFolder( myFolder.Path )
	For Each objMyFile In objMyFolder.Files
		strPath    = objMyFile.Path
		intMySize  = objMyFile.Size
		intFileAge = DateDiff( "d", objMyFile.DateLastModified, Now )
		If ( blnAge = False ) Or ( intFileAge > intAge ) Then
			If blnVerbose Then
				strMsg = strMsg & "Deleting file """ & strPath & """"
				If blnAge Then
					strMsg = strMsg & " (" & intFileAge & " days old)"
				End If
				strMsg = strMsg & vbCrLf
			End If
			On Error Resume Next
			objFSO.DeleteFile strPath, True
			If Err Then
				strMsg   = strMsg &  "Error # " & Err.Number & vbTab _
				         & Err.Description & vbTab & strPath & vbCrLf
				intFiles = intFiles + 1
				intSize  = intSize + intMySize
			End If
			On Error Goto 0
		Else
			If blnVerbose Then
				strMsg = strMsg & "Skipping file """ & strPath & """"
				If blnAge Then
					strMsg = strMsg & " (" & intFileAge & " days old)"
				End If
				strMsg = strMsg & vbCrLf
			End If
		End If
	Next
	For Each objSubFolder In objMyFolder.SubFolders
		DelTree objSubFolder, True
	Next
	If blnDeleteRoot Then
		strPath = objMyFolder.Path
		If objMyFolder.Files.Count = 0 And objMyFolder.SubFolders.Count = 0 Then
			If blnVerbose Then
				strMsg = strMsg &  "Deleting folder """ & strPath & """" & vbCrLf
			End If
			On Error Resume Next
			objFSO.DeleteFolder strPath, True
			If Err Then
				strMsg = strMsg &  "Error # " & Err.Number & vbTab _
				       & Err.Description & vbTab & strPath & vbCrLf
			End If
			On Error Goto 0
		Else
			If blnVerbose Then
				strMsg = strMsg &  "Skipping folder """ _
				       & strPath & """ (folder not empty)" & vbCrLf
			End If
		End If
	End If
	Set objMyFolder = Nothing
End Sub


Sub Syntax( )
	strMsg = vbCrLf _
	       & "DelTemp.vbs,  Version 1.11" & vbCrLf _
	       & "Empty TEMP directory, or remove only files older than the specified number" & vbCrLf _
           & "of days; and empty IE cache and optionally other shell folders too" & vbCrLf & vbCrLf _
	       & "Usage: DELTEMP.VBS  [ days | /S ]  [ /H ]  [ /Q | /V ]" & vbCrLf & vbCrLf _
	       & "Where: days  is the minimum age (in full calendar days) of TEMP files to be" & vbCrLf _
	       & "             deleted (0: delete every file older than today; blank: delete all)" & vbCrLf _
	       & "       /C    empty (IE) Cookies too" & vbCrLf _
	       & "       /D    empty Document History too" & vbCrLf _
	       & "       /F    empty (IE) Favorites too" & vbCrLf _
	       & "       /H    empty (IE) History too" & vbCrLf _
	       & "       /Q    quiet mode   : suppress all screen output" & vbCrLf _
	       & "       /S    safe mode    : don't delete TEMP files created after last reboot" & vbCrLf _
	       & "       /V    verbode mode : show lots of screen output" & vbCrLf & vbCrLf _
	       & "Notes: Close all programs before running this script, or use safe mode (/S)." & vbCrLf _
	       & "       This script will delete the temporary internet FILES, but IE's url" & vbCrLf _
	       & "       history can still be read from the index.dat files using strings.exe" & vbCrLf _
	       & "       http://www.microsoft.com/technet/sysinternals/Miscellaneous/Strings.mspx" & vbCrLf & vbCrLf _
	       & "Written by Michael Need" & vbCrLf _
	       & "http://www.internetbrilliance.com"
	WScript.Echo strMsg
	WScript.Quit 1
End Sub
