Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Batch file to delete temporary internet files and others

Posted on 2004-04-08
Medium Priority
Last Modified: 2012-05-04
I need to be able to clear the browser cache of temporary files, cookies, etc.  Ideally from a script so that it can be attached to a home page on the local machine.  It will always be invoked from a script on the local machine, not via a web page, but this seems to be extremely difficult

PeteLong answered a similar question on 11/11/03 by supplying source code to paste into Notepad, save as 'delete.vbs', then simply run the file.

The code works a dream when you run the code from a VB project in say Excel or other Office application, but does not work when I run the 'delete.vbs'; instead I get a VBS compilation error in line 3.  Ideally I want to run this as a script from the browser homepage so that it automatically clears the PC's cache whenever the user goes to their home page.  How can this be done, or failing that, what's the easiest way to make the script a straight executable which can be invoked from the homepage?  There seems no solution in JavaScript, so this VB (hopefully VBScript) seems the only possibility.

I predominantly code in JavaScript, so apologise if this is more straight forward in VBScriptFor reference I attach the source code below:

Option Explicit

Private Declare Function FindFirstUrlCacheGroup Lib "wininet.dll" ( _
   ByVal dwFlags As Long, _
   ByVal dwFilter As Long, _
   ByRef lpSearchCondition As Long, _
   ByVal dwSearchCondition As Long, _
   ByRef lpGroupId As Date, _
   ByRef lpReserved As Long) As Long

Private Declare Function FindNextUrlCacheGroup Lib "wininet.dll" ( _
   ByVal hFind As Long, _
   ByRef lpGroupId As Date, _
   ByRef lpReserved As Long) As Long
Private Declare Function DeleteUrlCacheGroup Lib "wininet.dll" ( _
   ByVal sGroupID As Date, _
   ByVal dwFlags As Long, _
   ByRef lpReserved As Long) As Long
Private Declare Function FindFirstUrlCacheEntry Lib "wininet.dll" Alias "FindFirstUrlCacheEntryA" ( _
   ByVal lpszUrlSearchPattern As String, _
   ByRef lpFirstCacheEntryInfo As INTERNET_CACHE_ENTRY_INFO, _
   ByRef lpdwFirstCacheEntryInfoBufferSize As Long) As Long
   dwStructSize As Long
   szRestOfData(1024) As Long
End Type

Private Declare Function DeleteUrlCacheEntry Lib "wininet.dll" Alias "DeleteUrlCacheEntryA" ( _
   ByVal lpszUrlName As Long) As Long

Private Declare Function FindNextUrlCacheEntry Lib "wininet.dll" Alias "FindNextUrlCacheEntryA" ( _
   ByVal hEnumHandle As Long, _
   ByRef lpNextCacheEntryInfo As INTERNET_CACHE_ENTRY_INFO, _
   ByRef lpdwNextCacheEntryInfoBufferSize As Long) As Long

Private Const ERROR_NO_MORE_FILES = 18
Private Const ERROR_NO_MORE_ITEMS = 259
Private Const BUFFERSIZE = 2048

Private Sub Command1_Click()
   Dim sGroupID As Date
   Dim hGroup As Long
   Dim hFile As Long
   Dim iSize As Long
   On Error Resume Next
   ' Delete the groups
   hGroup = FindFirstUrlCacheGroup(0, 0, 0, 0, sGroupID, 0)
   ' To avoid error using it with IE4 as FindFirstUrlCacheGroup is not implemented
   If Err.Number <> 453 Then
       If (hGroup = 0) And (Err.LastDllError <> 2) Then
           MsgBox "An error occurred enumerating the cache groups" & Err.LastDllError
           Exit Sub
       End If
   End If
   If (hGroup <> 0) Then
       'we succeeded in finding the first cache group.. enumerate and
           If (0 = DeleteUrlCacheGroup(sGroupID, CACHEGROUP_FLAG_FLUSHURL_ONDELETE, 0)) Then
               ' To avoid error using it with IE4 as FindFirstUrlCacheGroup is not implemented
              If Err.Number <> 453 Then
                MsgBox "Error deleting cache group " & Err.LastDllError
                Exit Sub
              End If
           End If
           iSize = BUFFERSIZE
           If (0 = FindNextUrlCacheGroup(hGroup, sGroupID, iSize)) And (Err.LastDllError <> 2) Then
               MsgBox "Error finding next url cache group! - " & Err.LastDllError
           End If
       Loop Until Err.LastDllError = 2
   End If
 ' Delete the files
   sEntryInfo.dwStructSize = 80
   hFile = FindFirstUrlCacheEntry(0, sEntryInfo, iSize)
   If (hFile = 0) Then
       If (Err.LastDllError = ERROR_NO_MORE_ITEMS) Then
           GoTo done
       End If
       MsgBox "ERROR: FindFirstUrlCacheEntry - " & Err.LastDllError
       Exit Sub
   End If
       If (0 = DeleteUrlCacheEntry(sEntryInfo.szRestOfData(0))) _
           And (Err.LastDllError <> 2) Then
       End If
       iSize = BUFFERSIZE
       If (0 = FindNextUrlCacheEntry(hFile, sEntryInfo, iSize)) And (Err.LastDllError <> ERROR_NO_MORE_ITEMS) Then
           MsgBox "Error:  Unable to find the next cache entry - " & Err.LastDllError
           Exit Sub
       End If
   Loop Until Err.LastDllError = ERROR_NO_MORE_ITEMS
   MsgBox "cache cleared"
   Command1.Enabled = True
End Sub
Question by:suzieMetcalf
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Accepted Solution

robinluo earned 375 total points
ID: 10785410
In IE properties, IE -> Tools -> Internet Options -> Advanced tab -> Security section, there is a feature called "Empty Temp Internet folder when browser close", can this feature fulfill what you are trying to do?

Author Comment

ID: 10816459
robinluo, sorry but I didn't see your comment until today.  The Empty Temp folder does not clear everything in the cache, certainly not persistent cookies which are the real concern.  This is why the VB code posted which comes from Microsoft is so efficient.  It clears all aspects of the cache; temp files, cookies, etc.  Also, it doesn't just remove these files, it actually removes them properly from the memory, and the various index.dat files.

Anyway, having spent many hours on this I have now worked out how to do it:  Using Microsoft's free VB5.0 CCE, I placed the code in an ActiveX control, and packaged it for the web.  You can then access the code anytime from Javascript by embedding the ActiveX control in the <Object> </Object> tags and invoking via a straightforward call.

It took ages to work this out, especially as knew nothing about VB or ActiveX before, but now works like a dream.  Can maintain version control by default with ActiveX if need to, using single repository for code, but most importantly it is now just a simple javascript function and can be placed in any page required.

Actually using as a corporate homepage for all employees which then hosts their selected homepage in a frame.  This way, will automatically clear the cache on launching browser or whenever go to homepage.

Hope that helps people out there.


Author Comment

ID: 10816470
robinluo, will also give you all the points as you were only one to get back to me!

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

NTFS file system has been developed by Microsoft that is widely used by Windows NT operating system and its advanced versions. It is the mostly used over FAT file system as it provides superior features like reliability, security, storage, efficienc…
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : All lightning effects with instructions : http://www.mediaf…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question