Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for deltemp3.vbs

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

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

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