1/19/11

Delete Internet Temp Files

'http://www.symantec.com/connect/blogs/deleting-temporary-files-and-folders

'=============================================
'File Name: DELETE_TEMP_FILES.VBS
'Comment: This script will delete all temporary files and folders
'=============================================

On Error Resume Next

'Declare variables
Dim fso
Dim oFolder1
Dim oFolder2
Dim oFolder3
Dim oSubFolder1
Dim oSubFolder2
Dim oSubFolder3
Dim colSubfolders1
Dim colSubfolders2
Dim colSubfolders3
Dim oFile
Dim userProfile
Dim Windir

'Set up environment
Set WSHShell = CreateObject("WScript.Shell")
Set fso = createobject("Scripting.FileSystemObject")
userProfile = WSHShell.ExpandEnvironmentStrings("%userprofile%")
Windir = WSHShell.ExpandEnvironmentStrings("%windir%")

'start deleting files
Set oFolder1 = fso.GetFolder(userProfile & "\Local Settings\Temp\")
For Each oFile In oFolder1.files
oFile.Delete True
Next
'Delete folders and subfolders
Set colSubfolders1 = oFolder1.Subfolders
On Error Resume Next
For Each oSubfolder in colSubfolders1
fso.DeleteFolder(oSubFolder), True
Next
Set oFolder2 = fso.GetFolder(userProfile & "\Local Settings\Temporary Internet Files\")
MsgBox userProfile & "\Local Settings\Temporary Internet Files\"

For Each oFile In oFolder2.files
'msgbox "1"
oFile.Delete True
'msgbox "2"
Next

'msgbox "3"

Set colSubfolders2 = oFolder2.SubFolders
For Each oSubfolder in colSubfolders2
fso.DeleteFolder(oSubFolder)
Next
'Set oFolder3 = fso.GetFolder(Windir & "\Temp\")
'For Each oFile In oFolder3.files
'oFile.Delete True
'Next

Set colSubfolders3 = oFolder1.Subfolders
For Each oSubfolder in colSubfolders3
fso.DeleteFolder(oSubFolder)
Next

'Clear memory
Set fso = Nothing
Set oFolder1 = Nothing
Set oFolder2 = Nothing
Set oFolder3 = Nothing
Set oSubFolder1 = Nothing
Set oSubFolder2 = Nothing
Set oSubFolder3 = Nothing
Set colSubfolders1 = Nothing
Set colSubfolders2 = Nothing
Set colSubfolders3 = Nothing
Set oFile = Nothing
Set userProfile = Nothing
Set Windir = Nothing

'Notify user that script has finished
MsgBox "Temporary files have been cleaned."

WScript.Quit

No comments: