Batch file to delete temporary internet files and others

Posted on 2004-04-08
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
  • 2

Accepted Solution

robinluo earned 125 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Windows 2003 Terminal Server licenses 3 362
win2k service packs 5 657
Making a spare domain pc 12 327
How do I clear an ActiveX Runtime Error 429 on NT workstation install.exe 7 415
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…
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

821 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