Solved

Batch file to delete temporary internet files and others

Posted on 2004-04-08
3
3,276 Views
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:

****************************************************************************************************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
*************************************************************************************
0
Comment
Question by:suzieMetcalf
  • 2
3 Comments
 
LVL 9

Accepted Solution

by:
robinluo earned 125 total points
Comment Utility
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?
0
 

Author Comment

by:suzieMetcalf
Comment Utility
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
0
 

Author Comment

by:suzieMetcalf
Comment Utility
robinluo, will also give you all the points as you were only one to get back to me!
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

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…
In this step by step tutorial with screenshots, we will show you HOW TO: Enable SSH Remote Access on a VMware vSphere Hypervisor 6.5 (ESXi 6.5). This is important if you need to enable SSH remote access for additional troubleshooting of the ESXi hos…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now