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

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

  • Comments 11
  • Likes
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!

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Yes... great, but how to make it work on 2003, vista and 7 with IE8?

  • I ran the script on Windows 7. UAC is turned off but getting a permission denied error even when running as administrator.

  • Hi All,

    I also have permission denied error.What can i do for that?

  • Running on Win 7  and added this with 2 additions, and they do not work.

    Const DeleteReadOnly = TRUE  

    After getting the folder path  added  ', DeleteReadOnly'  

    objFSO.DeleteFile(strPath), DeleteReadOnly

     I know that the Temp Internet Files folder is read only, so is the focus on changing the file attributes?

  • Please help !! It is not deleting temporary internet files on Windows 7 with 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)

  • Hey Scripting Guys... this article is from 2004. Any chance of a re-visit to take newer OS' into account?

  • :: Purpose:            Temp file cleanup

    :: Requirements:    Admin access

    :: Author:            Harbor Gate

    :: Version:            1.6

    :: History:            1.6 Changed some delete flags to /F /S /Q instead of just /F /Q

    ::                        The "/S" flag says to recurse into subdirectories.

    ::                    1.5 Added new areas to clean -- %TEMP%\ folder

    ::                    1.0 Initial write

    ::prepare

    @echo off

    color f0

    cd\

    cls

    title [CLEANING]

    :: User temp files

    del "%TEMP%\*" /F /S /Q

    :: Root drive garbage (usually C drive)

    del "%SystemDrive%\*.bat" /F /Q

    del "%SystemDrive%\*.txt" /F /Q

    del "%SystemDrive%\*.jp*" /F /Q

    del "%SystemDrive%\*.log*" /F /Q

    del "%SystemDrive%\*.t*mp" /F /Q

    del "%SystemDrive%\*.bak*" /F /Q

    :: System temp cache

    del "%WINDIR%\TEMP\*" /F /S /Q

    :: Flash cookies

    del %appdata%\Macromedia\Flash Player\#SharedObjects\*

    :: User history and temp files

    del "%SystemDrive%%HOMEPATH%\Recent\*.*" /F /Q

    del "%SystemDrive%%HOMEPATH%\Local Settings\Temp\*.*" /F /Q

    del "%SystemDrive%%HOMEPATH%\Local Settings\Temporary Internet Files\*.*" /F /Q

    del "%SystemDrive%%HOMEPATH%\Local Settings\Application Data\ApplicationHistory\*.*" /F /Q

    del "%SystemDrive%%HOMEPATH%\My Documents\*.tmp"

    del "%SystemDrive%%HOMEPATH%\My Documents\*.bak"

    del "%SystemDrive%%HOMEPATH%\My Documents\*.log"

    :: complete

    @echo off

    title [COMPLETE]

    echo.

    echo.

    echo Temp files cleaned.

    echo.

  •  Pratik  wrote a script in 04-10-2012 9:45 AM ... a while ago. It's a good start.

    --------------------------------------------------------------------------------------------

     Copied and tried it. Probably 1/2 of the 'del's work in the script for Win7 64Bit. The paths are there... but a lot of times there are user-security / file/directory visibility issues which prevent file erasure.

     Tried the 'runas' command, but that is basically a sham.

     There is a power toys option/command/workaround but more interested in using native DOS Batch commands to do this.

      Anybody have any hints or tips that would help accomplish this? The simpler the better.

  • I was able to get the script working by simply change the line: "objFSO.DeleteFile(strPath)" to "objFSO.DeleteFile strPath, DeleteReadOnly".

    Hope it works for you!

  • Use this one: just copy to notepad and save as:  deltemp.bat

    color 40

    @echo off

    ::Delete Temporary Internet Files

    RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 8

    ::Delete Cookies

    RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 2

    ::Delete History

    RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 1

    ::Delete Form Data

    RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 16

    ::Delete Passwords

    RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 32

    ::Delete All

    RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 255

    ::Delete All – “Also delete files and settings stored by add-ons”

    RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 4351

  • Hi, I need to delete temp internet files from specific domain, say google.com. Please let me know how can i do it.