Paypal Donate


This script goes through all local profiles and deletes temporary files (including temporary internet files).

It deletes files from the Local Settings/Temp folder, Local Settings/Temporary Internet Files, and Windows/Temp (assuming Windows is installed to the Windows folder).

To process all local profiles, the script needs to be run with admin rights - it can be used as a log-off or shut-down GPO script to keep machines free from temp files.  I tend to just run it every now and again, or schedule it once a week.

If you run the script from the command-line, either change the default script processor to cscript (means you don't get message pop-ups), or create a batch file that runs the script using cscript (as opposed to wscript).

IMPORTANT NOTE:  This script has only been tested under Windows 2000 and Windows XP.  Chances are it won't work under Vista and Windows 7, due to path changes and permissions.

' Script written by David Barrett
' Copyright 2009
' This script is licensed under the Creative Commons
' Attribution 2.5 Licence
' You are free to use it for both personal and
' commercial purposes, so long as full attribution
' is given to the author (David Barrett).
' This notice must not be removed


dim objWSH, sProfile, objFolder
dim objFSO, sProfileRoot, objProfileFolder
dim sTemp, sWindows

set objFSO=CreateObject("Scripting.FileSystemObject")

' Get user profile root folder
set objWSH    = CreateObject("WScript.Shell")
sTemp = objWSH.ExpandEnvironmentStrings("%TEMP%")
sWindows = objWSH.ExpandEnvironmentStrings("%WINDIR%")
sProfile = objWSH.ExpandEnvironmentStrings("%USERPROFILE%")
set objWSH=nothing

set objProfileFolder=objFSO.GetFolder(sProfileRoot)
for each objFolder in objProfileFolder.SubFolders
    select case LCase(objFolder.Name)
        case "all users": ' do nothing
        case "default user": ' do nothing
        case "localservice": ' do nothing
        case "networkservice": ' do nothing
        case else:
            wscript.echo "Processing profile: " & objFolder.Name
            sProfile=sProfileRoot & "\" & objFolder.Name
            DeleteFolderContents sProfile & "\Local Settings\Temp"
            DeleteFolderContents sProfile & "\Local Settings\Temporary Internet Files\Content.IE5"
            DeleteFolderContents sProfile & "\Local Settings\Temporary Internet Files"
    end select
' Now delete the folder given by the TEMP environment variable
wscript.echo "Processing folder: " & sTemp
DeleteFolderContents sTemp
' And the windows\temp folder
wscript.echo "Processing folder: " & sWindows & "\Temp"
DeleteFolderContents sWindows & "\Temp"

sub DeleteFolderContents(strFolder)
    ' Deletes all files and folders within the given folder
    dim objFolder, objFile, objSubFolder
    on error resume next
    set objFolder=objFSO.GetFolder(strFolder)
    if Err.Number<>0 then
        Exit sub ' Couldn't get a handle to the folder, so can't do anything
    end if
    for each objSubFolder in objFolder.SubFolders
        objSubFolder.Delete true
        if Err.Number<>0 then
            'Try recursive delete (ensures better result)
            DeleteFolderContents(strFolder & "\" & objSubFolder.Name)
        end if
    for each objFile in ObjFolder.Files
        objFile.Delete true
        if Err.Number<>0 then Err.Clear ' In case we couldn't delete a file
end sub

sub force_cscript
    dim args : args=""
    dim i, wshshell
    If right(lCase(wscript.fullname),11)= "wscript.exe" then
        for i=0 to wscript.arguments.count-1
            args = args & wscript.arguments(i) & " "
        set wshshell=createobject("") wshshell.ExpandEnvironmentStrings("%comspec%") & _
            " /c cscript.exe //nologo """ & wscript.scriptfullname & """" & args
        set wshshell=nothing
    end if
end sub

Copyright © 2009 All rights reserved.