Link to home
Start Free TrialLog in
Avatar of suzieMetcalf
suzieMetcalf

asked on

Batch file to delete temporary internet files and others

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:

****************************************************************************************************88
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
   
Private Type INTERNET_CACHE_ENTRY_INFO
   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 CACHGROUP_SEARCH_ALL = &H0
Private Const ERROR_NO_MORE_FILES = 18
Private Const ERROR_NO_MORE_ITEMS = 259
Private Const CACHEGROUP_FLAG_FLUSHURL_ONDELETE = &H2
Private Const BUFFERSIZE = 2048

Private Sub Command1_Click()
   Dim sGroupID As Date
   Dim hGroup As Long
   Dim hFile As Long
   Dim sEntryInfo As INTERNET_CACHE_ENTRY_INFO
   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
   Else
       Err.Clear
   End If
   
   If (hGroup <> 0) Then
       'we succeeded in finding the first cache group.. enumerate and
       'delete
       Do
           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
              Else
                 Err.Clear
              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
   iSize = BUFFERSIZE
   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
   Do
       If (0 = DeleteUrlCacheEntry(sEntryInfo.szRestOfData(0))) _
           And (Err.LastDllError <> 2) Then
           Err.Clear
       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
done:
   MsgBox "cache cleared"
   Command1.Enabled = True
End Sub
*************************************************************************************
ASKER CERTIFIED SOLUTION
Avatar of robinluo
robinluo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of suzieMetcalf
suzieMetcalf

ASKER

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.

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