Login to participate
Endpoint Management & Virtualization BlogsRSS

Deleting Temporary Files and Folders

Eshwar's picture

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

jbenjamin's picture

Faster Method

Windows Key + r
Type %temp%
hit enter. You can then hit the temp internet files by hitting up.

Ewentling's picture

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!

gsprague's picture

Tweaking

now i just have to modify it to delete the temp settings from every profile on the pc.

Eshwar's picture

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

Thanks,
Eshwar

RolandAY's picture

IS this vbscript

 Should we put this in notepad and save it As vbs?

Eshwar's picture

Yes, just copy the script to

Yes,
just copy the script to Notepad and save it with .vbs extension. Execute the script by double clicking on it.

If you want to execute the script accross several machines, you can do that using Altiris by creating a task to run the script.

I hope this helps.

Thanks,
Eshwar

Thanks,
Eshwar

Nitin Salvi's picture

Rep

Hi,

More you can click on start - run and tye %temp% and you will get all temp files and then simply select all and delete.

Eshwar's picture

Nitin, When ever we write

Nitin,
When ever we write scripts to automate regular tasks, we have to keep the intended audiance in mind. With that being said, it is easy to perform deletion operation individually. But the question araises when you want to automate this task in your organization.

In that scenario, you just have to write this vbscript and create a task in altiris and execute the task on the machines. Thats serves the purpose.

We run this script on a regular basis in our organization. I hope this may help.

Thanks,
Eshwar

Thanks,
Eshwar

Anvita's picture

Thanks

Wow.. Its simply great.. Thanks a lot Eshwar :)