Temporary directories in Windows are also one of the main reasons for undiagnosed program failures. A temporary directory littered with trash can cause applications to bomb out inexplicably, even after being removed and reinstalled.
For reasons such as these, it's best to keep Windows' temporary directories clean. However, few Windows admins want to make a manual cleanup part of their regular workload. Here are some ways to automate cleanup of the temporary directories in Windows.
Create a batch file that runs at login with the following commands:
rd /s /q %temp%
rd /s /q %systemroot%\temp
This action not only removes temporary directories but also recreates them as empty folders. Note: If the script encounters files that are in use, the whole delete operation for any given directory will abort. That's why it's best to run this script at logon when no other applications are yet running.
Some notes are in order here about the architecture of the Windows' temporary directories. There are two temporary directories that need policing: a directory in the user's profile and the system's temporary folder. The first folder's path will vary depending on the user name, but is revealed in the TEMP environment variable. If you type SET TEMP at a command line, you will see the 8.3 version of the pathname. The second is typically a folder named TEMP under the main system directory, which is also available through the %SystemRoot% variable.
Another option is to use a third-party program that performs certain cleaning functions automatically, such as the freeware CCleaner. You can set CCleaner to clean the TEMP directory in a user's profile, but it has to be added manually, and it can only be done when Explorer is set to show hidden directories. In CCleaner, click on Options in the left-hand menu bar, then Custom Folders in the Options area, then select Add Folder and navigate to the path to the current user's TEMP directory in his or her profile.
Once this is done, click Settings in Options, then select Run CCleaner when the computer starts to have this cleanup done automatically. Finally, in the Cleaner Settings window, select the Windows tab, and check the Custom Folders box at the bottom (under Advanced). If you're paranoid about whether or not certain files in the TEMP directory are still needed, click on Advanced in Options, then select Only delete files in Windows Temp folders older than 48 hours.
Note: You should not automate cleanups of Windows temporary directories if you're installing software that requires a reboot to complete the installation. For instance, programs that install one or more device drivers may have to do this. Many software installers use Windows temporary directories to hold the installer's files while the process completes. If those files are deleted, the installer won't be able to complete or may crash outright.
Note: If a program's installer crashes for reasons other than this, it may leave behind its setup files in the temp directory anyway. Those should be purged before attempting to run another program's installer, or there may be a conflict between the two sets of files.
Five Windows desktop tasks you should automate
How to automate Windows software updates
How to automate cleanup of Windows temporary directories
How to automate desktop folder synchronization
How to automate Windows desktop backup
How to automate advanced Windows desktop tasks
About the author:
Serdar Yegulalp is editor of the Windows Insight, (formerly the Windows Power Users Newsletter), a blog site devoted to hints, tips, tricks and news for users and administrators of Windows NT, Windows 2000, Windows XP, Windows Server 2003 and Vista. He has more than 12 years of Windows experience under his belt, and contributes regularly to SearchWinComputing.com and SearchSQLServer.com.
This was first published in November 2005