Doing a bit of system maintenance, I noticed some interesting stuff today about my %TEMP% folder.

The folder and its decedents are 13GB in size (that’s with a G and 9 zeros) and has 9,483 files!  This is almost equal to the “free space” on my drive.  Besides needing a bigger drive, this isn’t a good use of space on the machine.  Hum…

How’d it get there?  I’m not totally sure, but a big piece of it is debugging the streaming profiler.  After getting half way through profiling of an application – killing the profiler.  I do this alot.  The profiler tries to be a good citizen and clean up when it is done, but if you kill it, it never gets the chance.  I’ll also add that the profiler doesn’t delete on the way “in” as it ASSUMES that their might be other instances of the profiler running at the same time, so it doesn’t delete their stuff.

Back to me.  13GB wasted in temporary file system space!

XenApp hosted people have it easy.  There’s a configuration option to obliterate the TEMP folders on logoff.  I’m a domain based user using a machine that is part of the domain.  I don’t get the automatic delete on logoff and because I am not admin of the Citrix company domain, I also don’t have ability to set the setting to obliterate everything.  In an ideal world, I would have this Citrix Profile management stuff on my domain and all of this would be magically taken care of for me.  We will get there, but for now I still have 13GB wasted space!

What to do?  Just deleting it won’t be good enough – it will come back!

I went browsing the internet and found this nice article on how to clean house.  I can’t make deleting the TEMP space part of my system’s logoff, but I can command my machine to run batch files of my choosing at logoff.  Great!  If only I had a most-excellent set of batch files to help with the house cleaning.

A bit of coding and now I DO have these and I’m so happy with myself now that I now share the batch files with you.  Installation instructions are inside the batch file comments.

First bat file: logoff-script.bat@echo off

REM This script runs at every logoff for every user.

REM Enable by group policy (local policy)

REM

REM Start/Run GPEDIT.MSC

REM    User Configuration – Windows Settings

REM    Scripts – Logoff

REM    Add – Add this script.

setlocal

set OUTFILE=c:\logoff-script.log

REM Use a worker bat file to faciliate redirection of output.

call logoff-worker.bat | tee -a “%OUTFILE%”

endlocal

Second bat file: logoff-worker.bat@echo off

REM Called by logoff-script to faciliate redirecting output to log file

echo.

echo logoff-script running

date /t

time /t

echo Erasing TEMP directory

echo BEFORE

du -q “%TEMP%”

rd /s /q “%TEMP%” 1>nul: 2>&1

if not exist “%TEMP%\.” mkdir “%TEMP%”

echo AFTER

du -q “%TEMP%”

Things to notice.  First, I logged the output to the root of drive C:.  Mere user’s don’t have privilege to write to this space and on Vista, you’ll probably get a UAC dialog.  Easy to fix by writing to My Documents or similar.

In the main bat, there is a call to “tee -a”.  There are a million tee filters on the Internet.

In the worker, there’s a call to “du.exe” to log the before/after statistics.  This is the du.exe utility from Mark Russinovich (SysInternals).  Download from Microsoft Technet.  If you don’t have the DU utility and still want to log output, dir /s “%TEMP%” 2>&1 | tail -2.   Right, better to use the tool.

What about the registry?  If the file system has garbage left over from interrupted profiling, then the registry should also be poluted.  Went looking, nothing there.  Luck?  Probably not, but I uninstalled/reinstalled the profiler and client yesterday, so it is possible that the installer took care of this for me.  The place to look is HKLM\Software\Citrix\AIE, yes AIE!.  More information on the temporary spaces of the streaming profiler can be found here.

I’m not asking for a “mine is better than yours” tool comparison, but if you have a method that just plain makes this batch file stuff unnecessary, do post a comment to let me and others know.

Enjoy,

Joe Nord