Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for deltemp.vbs

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

  1. Option Explicit
  2.  
  3. Dim blnAge, blnCookies, blnDocHist, blnFavorites
  4. Dim blnIEHist, blnQuiet, blnSafe, blnVerbose
  5. Dim dtmLastBoot
  6. Dim intAge, intFiles, intParams, intSize
  7. Dim colItems, objFSO, objItem, objTempDir, objWMIService, wshShell
  8. Dim strMsg, strQuery, strTempDir
  9.  
  10. blnAge       = False
  11. blnCookies   = False
  12. blnFavorites = False
  13. blnIEHist   = False
  14. blnQuiet     = False
  15. blnSafe      = False
  16. blnVerbose   = False
  17. intAge       = -1
  18. intFiles     =  0
  19. intParams    =  0
  20. intSize      =  0
  21. strMsg       = ""
  22.  
  23. If WScript.Arguments.Unnamed.Count > 1 Then Syntax
  24. If WScript.Arguments.Unnamed.Count = 1 Then
  25. 	If IsNumeric( WScript.Arguments.Unnamed(0) ) Then
  26. 		blnAge = True
  27. 		intAge = CInt( WScript.Arguments.Unnamed(0) )
  28. 	Else
  29. 		Syntax
  30. 	End If
  31. End If
  32. If WScript.Arguments.Named.Exists( "W" ) Then
  33. 	blnWinTemp = True
  34. 	intParams  = intParams + 1
  35. End If
  36. If WScript.Arguments.Named.Exists( "V" ) Then
  37. 	blnVerbose = True
  38. 	intParams  = intParams + 1
  39. End If
  40. If WScript.Arguments.Named.Exists( "S" ) Then
  41. 	If blnAge Then
  42. 		Syntax ' days (age) and /S are mutually exclusive
  43. 	Else
  44. 		blnSafe   = True
  45. 		intParams = intParams + 1
  46. 	End If
  47. End If
  48. If WScript.Arguments.Named.Exists( "Q" ) Then
  49. 	If blnVerbose Then
  50. 		Syntax ' /Q and /V are mutually exclusive
  51. 	Else
  52. 		blnQuiet  = True
  53. 		intParams = intParams + 1
  54. 	End If
  55. End If
  56. If WScript.Arguments.Named.Exists( "C" ) Then
  57. 	blnCookies = True
  58. 	intParams  = intParams + 1
  59. End If
  60. If WScript.Arguments.Named.Exists( "D" ) Then
  61. 	blnDocHist = True
  62. 	intParams  = intParams + 1
  63. End If
  64. If WScript.Arguments.Named.Exists( "F" ) Then
  65. 	blnFavorites = True
  66. 	intParams    = intParams + 1
  67. End If
  68. If WScript.Arguments.Named.Exists( "H" ) Then
  69. 	blnIEHist = True
  70. 	intParams = intParams + 1
  71. End If
  72. If WScript.Arguments.Named.Exists( "A" ) Then
  73. 	blnCookies   = True
  74. 	blnDocHist   = True
  75. 	blnFavorites = True
  76. 	blnIEHist    = True
  77. 	intParams    = intParams + 4
  78. End If
  79. If intParams < WScript.Arguments.Named.Count Then Syntax
  80.  
  81. Set objFSO   = CreateObject( "Scripting.FileSystemObject" )
  82. Set wshShell = Wscript.CreateObject( "Wscript.Shell" )
  83.  
  84. DelShellFolder "Cache"                           ' Empty (IE) Cache
  85. If blnIEHist    Then DelShellFolder "History"    ' Empty (IE) History
  86. If blnDocHist   Then DelShellFolder "Recent"     ' Empty Documents history
  87. If blnCookies   Then DelShellFolder "Cookies"    ' Remove (IE) Cookies
  88. If blnFavorites Then DelShellFolder "Favorites"  ' Remove (IE) Favorites
  89.  
  90. ' Check if safe mode was requested (for TEMP directory only)
  91. If blnSafe Then
  92. 	strQuery = "Select * from Win32_OperatingSystem"
  93. 	Set objWMIService = GetObject( "winmgmts://./root/cimv2" )
  94. 	Set colItems      = objWMIService.ExecQuery( strQuery, , 48 )
  95. 	For Each objItem In colItems
  96. 		dtmLastBoot = objItem.LastBootUpTime
  97. 	Next
  98. 	Set colItems      = Nothing
  99. 	Set objWMIService = Nothing
  100. 	blnAge      = True
  101. 	dtmLastBoot = CDate( Mid( dtmLastBoot, 7, 2 ) & " " _
  102. 	            & MonthName( Mid( dtmLastBoot, 5, 2 ) ) & " " _
  103. 	            & Left( dtmLastBoot, 4 ) )
  104. 	intAge      = DateDiff( "d", dtmLastBoot, Now )
  105. End If
  106.  
  107. strTempDir     = wshShell.ExpandEnvironmentStrings( "%TEMP%" )
  108. Set objTempDir = objFSO.GetFolder( strTempDir )
  109. DelTree objTempDir, False
  110. Set objTempDir = Nothing
  111.  
  112. strTempDir     = wshShell.ExpandEnvironmentStrings( "%windir%\Temp" )
  113. Set objTempDir = objFSO.GetFolder( strTempDir )
  114. DelTree objTempDir, False
  115. Set objTempDir = Nothing
  116.  
  117. If intFiles > 0 Then
  118. 	strMsg = strMsg & intFiles & " file"
  119. 	If intFiles > 1 Then strMsg = strMsg & "s"
  120. 	strMsg = strMsg & " could not be deleted (" _
  121. 	       & FormatNumber( intSize / 1048576, 1, True, False, False ) _
  122. 	       & " MB)"
  123. End If
  124.  
  125. If Not blnQuiet Then WScript.Echo strMsg
  126.  
  127. Set objFSO   = Nothing
  128. Set wshShell = Nothing
  129.  
  130.  
  131. Sub DelShellFolder( myShellFolder )
  132. 	Dim objShellFolder, strRegKey, strShellFolder
  133. 	strRegKey      = "HKEY_CURRENT_USER\Software\Microsoft\Windows\" _
  134. 	               & "CurrentVersion\Explorer\Shell Folders\" & myShellFolder
  135. 	strShellFolder = wshShell.RegRead( strRegKey )
  136. 	Set objShellFolder = objFSO.GetFolder( strShellFolder )
  137. 	DelTree objShellFolder, False
  138. 	Set objShellFolder = Nothing
  139. End Sub
  140.  
  141.  
  142. Sub DelTree( ByRef myFolder, blnDeleteRoot )
  143. 	Dim intFileAge, intMySize, objMyFile, objMyFolder, objSubFolder, strPath
  144. 	Set objMyFolder = objFSO.GetFolder( myFolder.Path )
  145. 	For Each objMyFile In objMyFolder.Files
  146. 		strPath    = objMyFile.Path
  147. 		intMySize  = objMyFile.Size
  148. 		intFileAge = DateDiff( "d", objMyFile.DateLastModified, Now )
  149. 		If ( blnAge = False ) Or ( intFileAge > intAge ) Then
  150. 			If blnVerbose Then
  151. 				strMsg = strMsg & "Deleting file """ & strPath & """"
  152. 				If blnAge Then
  153. 					strMsg = strMsg & " (" & intFileAge & " days old)"
  154. 				End If
  155. 				strMsg = strMsg & vbCrLf
  156. 			End If
  157. 			On Error Resume Next
  158. 			objFSO.DeleteFile strPath, True
  159. 			If Err Then
  160. 				strMsg   = strMsg &  "Error # " & Err.Number & vbTab _
  161. 				         & Err.Description & vbTab & strPath & vbCrLf
  162. 				intFiles = intFiles + 1
  163. 				intSize  = intSize + intMySize
  164. 			End If
  165. 			On Error Goto 0
  166. 		Else
  167. 			If blnVerbose Then
  168. 				strMsg = strMsg & "Skipping file """ & strPath & """"
  169. 				If blnAge Then
  170. 					strMsg = strMsg & " (" & intFileAge & " days old)"
  171. 				End If
  172. 				strMsg = strMsg & vbCrLf
  173. 			End If
  174. 		End If
  175. 	Next
  176. 	For Each objSubFolder In objMyFolder.SubFolders
  177. 		DelTree objSubFolder, True
  178. 	Next
  179. 	If blnDeleteRoot Then
  180. 		strPath = objMyFolder.Path
  181. 		If objMyFolder.Files.Count = 0 And objMyFolder.SubFolders.Count = 0 Then
  182. 			If blnVerbose Then
  183. 				strMsg = strMsg &  "Deleting folder """ & strPath & """" & vbCrLf
  184. 			End If
  185. 			On Error Resume Next
  186. 			objFSO.DeleteFolder strPath, True
  187. 			If Err Then
  188. 				strMsg = strMsg &  "Error # " & Err.Number & vbTab _
  189. 				       & Err.Description & vbTab & strPath & vbCrLf
  190. 			End If
  191. 			On Error Goto 0
  192. 		Else
  193. 			If blnVerbose Then
  194. 				strMsg = strMsg &  "Skipping folder """ _
  195. 				       & strPath & """ (folder not empty)" & vbCrLf
  196. 			End If
  197. 		End If
  198. 	End If
  199. 	Set objMyFolder = Nothing
  200. End Sub
  201.  
  202.  
  203. Sub Syntax( )
  204. 	strMsg = vbCrLf _
  205. 	       & "DelTemp.vbs,  Version 2.00" & vbCrLf _
  206. 	       & "Empty TEMP directory, or remove only files older than the specified number" & vbCrLf _
  207.            & "of days; and empty IE cache and optionally other shell folders too" & vbCrLf & vbCrLf _
  208. 	       & "Usage: DELTEMP.VBS [days|/S] [/C] [/D] [/F] [/H] [/W] [/Q|/V]" & vbCrLf & vbCrLf _
  209. 	       & "Where: days  is the minimum age (in full calendar days) of TEMP files to be" & vbCrLf _
  210. 	       & "             deleted (0: delete every file older than today; blank: delete all)" & vbCrLf _
  211. 	       & "       /A    same as /C /D /F /H" & vbCrLf _
  212. 	       & "       /C    empty (IE) Cookies too" & vbCrLf _
  213. 	       & "       /D    empty Document History too" & vbCrLf _
  214. 	       & "       /F    empty (IE) Favorites too" & vbCrLf _
  215. 	       & "       /H    empty (IE) History too" & vbCrLf _
  216. 	       & "       /Q    quiet mode   : suppress all screen output" & vbCrLf _
  217. 	       & "       /S    safe mode    : don't delete TEMP files created after last reboot" & vbCrLf _
  218. 	       & "       /V    verbode mode : generate lots of screen output" & vbCrLf & vbCrLf _
  219. 	       & "Notes: This script deletes temporary internet files, not IE's url history." & vbCrLf _
  220. 	       & "       Close all programs before running this script, or use safe mode (/S)" & vbCrLf & vbCrLf _
  221. 	       & "Written by Rob van der Woude" & vbCrLf _
  222. 	       & "http://www.robvanderwoude.com"
  223. 	WScript.Echo strMsg
  224. 	WScript.Quit 1
  225. End Sub
  226.  

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