How Can I Delete the Files in the Temporary Internet Files Folder?

Hey, Scripting Guy! Question

Hey, Scripting Guy! Is there a way to delete all the files in the Temporary Internet Files folder using a script?

— KR

SpacerHey, Scripting Guy! AnswerScript Center

Hey, AK. You know, prompted by your question one of the Scripting Guys (who shall remain nameless) thought to himself, “Hmmm, I wonder what’s in my Temporary Internet Files folder?” Upon checking, he discovered the folder contained 239 megabytes worth of junk; as near as he could tell, the entire Internet was being stored on his hard disk.


In other words, having a script that deletes all the files in the Temporary Internet Files folder sounded like a script worth having.


The only complicating factor is trying to determine where a user has his or her Temporary Internet Files folder. Typically, you’ll find this in the user’s local user profile, which will usually be something like C:\Documents and Settings\kenmyer\Local Settings\Temporary Internet Files. However, the folder doesn’t have to be there; after all, Windows might not even be installed on drive C. That means that the key to writing this script lies in locating the Temporary Internet Files folder; after you’ve found it, deleting all the files is a snap.


So how do you locate this folder? Well, as it turns out, the Temporary Internet Files folder is a “special” folder, a folder which is found, by default, on all installations of Windows, and a folder that the operating system keeps an eye on; no matter what you do to this folder, Windows will still know its whereabouts. Because of that, you can use the Shell object – which has the ability to locate special folders, regardless of their physical location – to determine the path to Temporary Internet Files, and then use that path to bind to the folder and delete all the files found there.


One caveat: the Shell object can’t be created remotely. That means you either need to run this script as a logon or logoff script, or copy it to the remote computer and use the WMI Win32_Process class to kick it off on that remote machine. For more information, you might want to see this Hey, Scripting Guy! column, which used Win32_Process to run the md command on a remote computer.


Let’s take a look at the script:

Const TEMPORARY_INTERNET_FILES = &H20&

Set objShell = CreateObject(“Shell.Application”)
Set objFolder = objShell.Namespace(TEMPORARY_INTERNET_FILES)
Set objFolderItem = objFolder.Self
strPath = objFolderItem.Path & “\*.*”

Set objFSO = CreateObject(“Scripting.FileSystemObject”)
objFSO.DeleteFile(strPath)


Yes, we know: after that big buildup, you thought this would be a lot more complicated, didn’t you? We start off by setting the constant TEMPORARY_INTERNET_FILES to &H20&, the value required to connect to the Temporary Internet Files folder (more on that in a minute). We then create the Shell object, and use the Namespace method to locate the folder. Due to the somewhat quirky nature of the Shell object, we then call the Self method to actually connect to the folder; that’s what this line of code does:


Set objFolderItem = objFolder.Self


At this point, we can now determine the actual path (e.g., C:\Documents and Settings\kenmyer\Local Settings\Temporary Internet Files) to the folder in question. If that’s all we wanted to know, we could just use a line of code similar to this:

Wscript.Echo objFolderItem.Path

However, we want to use the FileSystemObject to delete all the files in that folder. To do that, we have to pass the FileSystemObject a path similar to this, using standard wildcard characters to represent all the files in the folder:

C:\Documents and Settings\kenmyer\Local Settings\Temporary Internet Files\*.*

And then we build that path with this line of code:

strPath = objFolderItem.Path & “\*.*”

As you can see, we take the path returned by the Shell object, and then we add \*.*. The net result is a command which will tell the FileSystemObject to delete all the files in the folder.


So that’s what we do in the final two lines of code: we create an instance of the FileSystemObject, and then use the DeleteFile method to delete all the files. Just like that, we’ve cleaned out our Temporary Internet Files Folder.


Of course, as long as you’re at it, you might want to clean out the Internet Cookies folder as well. Here’s a script that will do that:

Const COOKIES = &H21&

Set objShell = CreateObject(“Shell.Application”)
Set objFolder = objShell.Namespace(COOKIES)
Set objFolderItem = objFolder.Self
strPath = objFolderItem.Path & “\*.*”

Set objFSO = CreateObject(“Scripting.FileSystemObject”)
objFSO.DeleteFile(strPath)


Pretty cool, huh?


Incidentally, we had a premonition that as soon as we posted this column people would start asking us, “Say, how do I connect to the My Pictures folder?” or “Is there any way to connect to the My Recent Documents folder?” Because of that, we went ahead added 38 new scripts to the Script Center Script Repository, scripts that will show you how to connect to each of the special folders reachable using the Shell object. Enjoy!