Scripting Unattended Disk Cleanup & Defrag

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…

Disk Cleanup
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

Disk Defrag
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
About these ads

  1. #1 by dKarl on June 8, 2009 - 5:18 am

    How do you easily run a defrag command or registry key?

  2. #2 by Nicolas Moseley on June 9, 2009 - 11:28 am

    Thanks for the comment dKarl – I noticed the WordPress post was not wrapping the text, but cutting if off instead. I added a link to the document you can use to copy/paste.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 59 other followers

%d bloggers like this: