Deleting Temporary Files and Folders
Filed under:
WiseScript
Scripting
Submitted by eshwar on 7 July, 2008 - 13:31.
You can manually delete temporary files and folders from the following locations:
[C:\Documents and Settings\<username>\Local Settings\Temp]
[C:\Documents and Settings\<username>\Local Settings\Temporary Internet Files]
You can ignore the above method since there is a smart way to do it. Just execute the following script and you are done.
'=============================================
'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\")
For Each oFile In oFolder2.files
oFile.Delete True
Next
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
WScript.Quit
(34 votes)
- Login or register to post comments
- 2744 reads
- Printer-friendly version















Faster Method
Windows Key + r
Type %temp%
hit enter. You can then hit the temp internet files by hitting up.
A small change
I have often been at a users desk cleaning out the temp folders and have been asked for a simple way for them to so it themselves. This is a great solution. I made one small change to the script to let the user know the script has done anything. Insert the following lines before WScript.Quit
'Notify user that script has finished
MsgBox "Temporary files have been cleaned."
Thanks for the great submission!
Tweaking
now i just have to modify it to delete the temp settings from every profile on the pc.
sample Script
You just need to execute the above script for every userprofile on the machine. Check the following user profile script:
Dim sDocsAndSettings Dim strComputer Dim oFolder strComputer = "." Set fso = createobject("Scripting.FileSystemObject") 'USER PROFILES sDocsAndSettings = "C:\Documents and Settings\" ' Probably there is an environment variable or other variable Set colFolders = fso.GetFolder(sDocsAndSettings) 'Collection of all profiles For Each oFolder In colFolders.SubFolders Select Case LCase(oFolder.Name) Case "admin", "administrator", "newuser", "all users", "default user.original", "localservice", "networkservice" 'DO NOTHING Case Else '======================================= 'DELETE TEMP FILES SCRIPT '======================================= End If End Select Next