Video Screencast Help
Search Video Help Close Back
to help
New in the Rewards Catalog: Vouchers for "Symantec Technical Specialist" and "Symantec Certified Specialist" exams.

Deleting Temporary Files and Folders

Updated: 07 Jul 2008 | 14 comments
Eshwar's picture
+28 28 Votes
Login to vote

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

Comments

jbenjamin's picture
07
Jul
2008
0 Votes 0
Login to vote

Faster Method

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

Ewentling's picture
14
Jul
2008
1 Vote +1
Login to vote

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
30
Jul
2008
0 Votes 0
Login to vote

Tweaking

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

Eshwar's picture
31
Jul
2008
14 Votes +14
Login to vote

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
30
Jun
2009
0 Votes 0
Login to vote

IS this vbscript

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

Eshwar's picture
03
Jul
2009
1 Vote +1
Login to vote

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
01
Jul
2009
0 Votes 0
Login to vote

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
03
Jul
2009
2 Votes +2
Login to vote

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
27
Jul
2009
0 Votes 0
Login to vote

Thanks

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

souls1211's picture
12
Feb
2010
0 Votes 0
Login to vote

Great script.. How would I

Great script.. How would I take out deleting the contents of the temp folder. I only need to delete temp files in my org Thanks,

hegwig's picture
06
Jun
2010
0 Votes 0
Login to vote

1/4 million temporary IE files

This is an old thread, but it still pertains. People don't realize how fast those temporaty IE files can turn into a breeding area for viruses, worms, adware, etc. Recently, a friend gave me her father's computer which had crashed and failed to boot. The error msg was "corupted directory structure". I made an image of the boot partition and dumped the whole 19 GB mess onto a portable USB drive. Then I used ARAX Disk Doctor to recover the file structure with folders and files intact. Then I scanned it with NIS. After a long while, the scan was complete. There were two virus files and over 500,000 total files. Over half of the files were in temporary IE folders! Tens of thousands more in the System Volume Information folder. Obviously the viruses had no trouble hiding in there. Now I need to find a utility to delete temp IE files on an inactive (non-startup) partition. Maybe the above script (with modiication) will work. I don't think it will boot again though.

marriealen's picture
21
Nov
2010
0 Votes 0
Login to vote

I think it is all due to long

I think it is all due to long path error in file system,i will suggest you to use a tool available at http://pathtoodeep.com

garywm's picture
30
Jan
2011
0 Votes 0
Login to vote

deleting from every profile but don't know name of folders

Hi,

I don't know if this thread is still being followed but what if the user profiles under Documents and Settings are unknown.  Is there a way to look through all of them and delete the contents of a folder?

Thanks,

garywm

Ian_C.'s picture
31
Jan
2011
0 Votes 0
Login to vote

Temp files & more

Dear garywm.

You can use the following two lines to do this for you.

FOR /d %d in ("%SYSTEMDRIVE%\Documents and Settings\*") do RD "%d\Local Settings\Temp" /s /q >nul
FOR /d %d in ("%SYSTEMDRIVE%\Documents and Settings\*") do MD "%d\Local Settings\Temp" >nul 

You can run those commands directly from a DOS Prompt. If you want this in a batch file (.bat) then the %d needs to change to %%d like this

FOR /d %%d in ("%SYSTEMDRIVE%\Documents and Settings\*") do RD "%%d\Local Settings\Temp" /s /q >nul
FOR /d %%d in ("%SYSTEMDRIVE%\Documents and Settings\*") do MD "%%d\Local Settings\Temp" >nul 

 

The thing though is, there are many more temp locations than just these two discussed above. Copy the below text into Notepad and save with a .bat extension:

@ECHO off
TITLE Cleaning up TEMP directories
REM C:\Windows\Temp dir

RD %Systemroot%\Temp /s /q >nul
IF not exist %Systemroot%\Temp MD %Systemroot%\Temp

REM C:\Temp dir

RD %SystemDrive%\Temp /s /q
MD %SystemDrive%\Temp

REM Log files in C:\ and Windows directory

DEL %Systemroot%\Modemlog*.txt /q /f >nul
DEL %Systemroot%\*.log /q /f >nul
DEL %SystemDrive%\*.log /q /f >nul

REM Clean up user profiles temp dirs

FOR /d %%d in ("%SYSTEMDRIVE%\Documents and Settings\*") do RD "%%d\Local Settings\Temp" /s /q >nul
FOR /d %%d in ("%SYSTEMDRIVE%\Documents and Settings\*") do MD "%%d\Local Settings\Temp" >nul

DEL %WINDIR%\MiniDump\*.* /q /f >nul

REM Delete all hotfix uninstall files
REM This can get very big if the PC is more than a year old. (1.5GB+)
ATTRIB -h %WinDir%\$NtUninstall* /s /d
FOR /d %%k in ("%WinDir%\$NtUninstall*") do RD "%%k" /s /q >nul 

 

Hope that helps.