I was recently tasked with automating a disk cleanup and defrag for the XP workstations in our environment. Simple enough of a task using the built-in disk cleanup and defrag. But I thought I’d share some details of how I automated them…
Grasping how to peforming an unattended cleanup can be tricky. The first thing to understand is that selecting the various caches (Internet files, temporary files, trash, etc etc) is controlled through a “sageset”. You can think of a sageset as the configuration number which inidicates what diskclean should remove. So one could have a sageset which cleans up only trash, another sageset which cleans up trash and internet files, another sageset which cleans up all items except memory dump files, etc etc.
This sageset are DWORD values in HKLM\Software\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\ in each subkey. For example, I want to cleanup only Internet Cache Files and assign sageset 99 to it. Within subkey “Internet Cache Files” I create the DWORD value name StateFlags0099 and give it the value 2. Then to cleanup this item, I simply run cleanmgr.exe /sagerun:99 – That’s it! To add more items to the sageset, all one needs do is add that StateFlags0000 (where the zeos are the sageset number created).
Make sense? For more information, see Q315246
Not too much doin’ here. I simply trigger defrag.exe c: -f
A neat thing about defrag is that you can first have it analyze the disk to see its fragmentation stats. Since I log SCCM installs/operations for workstations into a custom log file, I thought it would good to capture this analysis in the log file. Below is the vbscript code that I used to facilitate this…
Dim oFSO, oShell, oLogFile Const cForAppending = 8 Set oFSO = CreateObject("Scripting.FileSystemObject") Set oShell = CreateObject("WScript.Shell") Set oLogFile = oFSO.OpenTextFile ("C:\CustomLog.txt", cForAppending, True) Set oExecRun = oShell.Exec ("C:\Windows\system32\defrag.exe c: -a") ' Reads through the output of the analysis until it comes to Analysis Report... ' Then it writes the next line read into the log file Do While Not oExecRun.StdOut.AtEndOfStream If oExecRun.StdOut.ReadLine = "Analysis Report " Then 'extra spaces after report are critical! oLogFile.WriteLine " => Defrag Analysis Report:" & oExecRun.StdOut.ReadLine End If Loop oLogFile.Close