Subscribe to Windows IT Pro
October 06, 2003 12:00 AM

Clear Out Cached Data

A simple Perl script cleans up
Windows IT Pro
InstantDoc ID #40232
Rating: (0)
Downloads
40232.zip

Today's user-friendly programs and OSs often provide users instant access to recently used data and documents through cached Web pages, type-ahead lists, and most recently used (MRU) lists. All these features are helpful but can consume a great deal of disk space and can become a burden for systems administrators who need to protect users' privacy. When such problems arise, you can use the Perl script CleanCache.pl, which Web Listing 1 (http://www.winscriptingsolutions.com, InstantDoc ID 40232) shows, to clean out many of the most common Windows file caches and MRU lists. (This article assumes that you have a basic understanding of Perl; for more information about that language, see http://www.roth.net/perl.)

Internet Explorer Cache
When you use Microsoft Internet Explorer (IE) to surf the Web, IE stores Web pages, graphics, cookies, Java scripts, and other downloaded content on your hard disk in a location known as the IE cache. Subsequent requests for such stored objects don't require you to reconnect to a Web server. Thus, you save connection and download time, especially for large downloadable files. The cache doesn't belong only to IE, however; rather, it belongs to a library known as WinInet.dll. This library provides Web, FTP, and gopher protocol access as well as caching functionality. IE and other applications, such as Microsoft Outlook Express and Windows Media Player (WMP), use the WinInet cache, so the cache can contain interesting data files. To view the list of items in the cache, open the Control Panel Internet Options applet, go to the General tab, and click Settings (under Temporary Internet files). Then, click View Files in the Settings dialog box.

The WinInet cache consists of two components: a database and a repository. The repository is a directory or collection of directories in which the cached files reside. The database contains entries that map each URL to a cached item's location on disk. When you download a file from the Web, IE stores the file in the repository and adds a pointer entry to the database. Typically, the cache repository is under %systemroot%\documents and settings\username\local settings\temporary internet files, where username is the name of the user.

Clearing the cache is fairly simple—you just need to remove the cached files from the hard disk's repository (typically the Temporary Internet Files directory) and remove the entries from the database. However, cache database entries can map not only to the repository but also to any file in any directory on any drive on the machine or even on a remote machine. Therefore, if you simply delete all the files in the Temporary Internet Files directory, you might miss some cached files.

In addition, the WinInet cache can store cached files individually or as a group. Therefore, any script that cleans out the cache should query the cache database for both individual files and groups of files. For example, WMP can cache content when streaming from a media server or progressively download content from a Web server. WMP stores these files as a group in the WinInet cache. Therefore, if a script searches for and deletes only files, the script might not remove the group of cached WMP files. Along the same lines, to delete only WMP media files, you need a script that will recognize cached group files.

Furthermore, deleting cached files doesn't clean out the cache database, which an application (e.g., IE) or script can still query. To properly clean out the cache, you need to enumerate all entries in the database, then delete each cached file and remove the related entry from the database. The WinInet library exposes functions that enumerate each cache entry and a function—DeleteUrlCacheEntry()—that both deletes the cached file and removes the entry from the cache database. (The same procedure is necessary to remove cached cookies and browser history information.) Because multiple processes can use the WinInet cache simultaneously, the database files are usually open. Consequently, a script can't delete the actual database files. Rather, the script must simply delete information from the database files.

Temporary Files
Applications generate all sorts of temporary files. Although some applications automatically delete their temporary files when they shut down, many applications don't remove these files. (Microsoft Word is notorious for creating temporary files and never cleaning them out.)

Over time, lingering temporary files can take up considerable space, ranging from several kilobytes to several hundreds of megabytes or more. Worse yet, these files are generally small and can lead to fragmentation of larger files. Severe file fragmentation can degrade overall system performance (especially if the Windows pagefile becomes fragmented) and cause undue wear and tear on your hard disk. Therefore, cleaning out the temporary file directories from time to time is a good idea.

Related Content:

ARTICLE TOOLS

Comments
    There are no comments to display. Be the first one!
You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.