Winhelponline.com - http://www.winhelponline.com
How to schedule a defrag so that it runs only when needed
http://www.winhelponline.com/articles/74/1/How-to-schedule-a-defrag-so-that-it-runs-only-when-needed.html
By Ramesh Srinivasan
Published on September 10, 2006
 
The command-line Defrag program (defrag.exe) supports /a switch, which can analyze the specified volume, and notifies you if  defragmentation for that volume is necessary or not.

How to schedule a defrag so that it runs only when needed

Introduction

The command-line Defrag program (defrag.exe) supports /a switch, which can analyze the specified volume, and notifies you if  defragmentation for that volume is necessary or not. Here is a sample output, when used the /a switch is used:

Windows Disk Defragmenter
Copyright (c) 2001 Microsoft Corp. and Executive Software International, Inc.

Analysis Report
3.90 GB Total, 3.05 GB (78%) Free, 0% Fragmented (0% file fragmentation)

You do not need to defragment this volume.

Now, how to read the output programmatically, and then invoke the defragmentation task, depending upon the Analysis Report? This article explains how to do so.

Solution

Use the VBScript below, which invokes the defrag analysis for each drive, and reads the Analysis Report to know if the drive needs to be defragmented or not. The step is repeated for every fixed disk present in the system.

'---------------------------------------------------------
'Author : Ramesh Srinivasan
'Dated : September 10, '06
'Desc : Defrag all the drives, only if fragmented.
'Desc : The script reads the Defrag Analysis Report to ..
'know if the drive(s) need to be defragmented.
'---------------------------------------------------------
'Copyright 2006 Ramesh Srinivasan. All rights Reserved.
'Windows XP Troubleshooting: http://www.winhelponline.com
'---------------------------------------------------------

Set objShell = CreateObject("Wscript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
strSearch = "You do not need to defragment this volume"
Const MIN_INACTIVE = 7

Set colDrives = objFSO.Drives
For Each objDrive in colDrives
If objDrive.DriveType = 2 Then
Set objWshScriptExec = objShell.Exec("defrag.exe " _
& objDrive.DriveLetter & ": -a")
strOutput=objWshScriptExec.StdOut.ReadAll
If InStr(1, strOutput, strSearch) = 0 Then
objShell.Run "defrag.exe " _
& objDrive.DriveLetter & ":", MIN_INACTIVE ,True
End If
End If
Next

Set objShell = Nothing
Set objFSO = Nothing

Copy the above contents and Paste it in Notepad. Save the file with .VBS extension (say, dfrg.vbs). You can then double-click the file to run it, or, you can even schedule this script using Windows Task Scheduler.

The above code enumerates all the fixed drive letters. To defragment one particular drive only, use this code:

'---------------------------------------------------------
'Author : Ramesh Srinivasan
'Dated : September 10, '06
'Desc : Defrag C: drive, only if fragmented.
'Desc : The script reads the Defrag Analysis Report to ..
'know if the C: drive need to be defragmented.
'---------------------------------------------------------
'Copyright 2006 Ramesh Srinivasan. All rights Reserved.
'Windows XP Troubleshooting: http://www.winhelponline.com
'---------------------------------------------------------

Set objShell = CreateObject("Wscript.Shell")
Set objWshScriptExec = objShell.Exec("defrag.exe c: -a")
strOutput=objWshScriptExec.StdOut.ReadAll
strSearch = "You do not need to defragment this volume"
If InStr(1, strOutput, strSearch) = 0 Then
objShell.Run ("defrag.exe c:")
End If