Home > Windows Server Tips > > How to automate cleanup of Windows temporary directories
Windows Server Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 


How to automate cleanup of Windows temporary directories


Serdar Yegulalp, Contributor
11.14.2005
Rating: -3.58- (out of 5)


Expert advice on Windows-based systems and hardware
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


Windows' temporary directories are dumping grounds for nearly every program that does more than trivial activity.

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%
md %temp%
rd /s /q %systemroot%\temp
md %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

  Introduction
 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.


Rate this Tip
To rate tips, you must be a member of SearchWindowsServer.com.
Register now to start rating these tips. Log in if you are already a member.




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.

HomeTopicsITKnowledge ExchangeTipsAsk the ExpertsMultimediaWhite PapersIT Downloads
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 2004 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts