Automatic Defrag Vbs Script.

Disk fragmentation causes crashes, slowdowns, freeze-ups and even total system failures. The number one reason for performance bottlenecks is fragmentation. Even the best hardware will eventually slow down unless the drive is defragmented daily.

The disk drive is by far the slowest of the three main components of your computer: CPU, memory and disk. If the drive isn’t defragmented the fastest CPU in the world won’t improve your system’s performance, because information from the disk simply can’t be delivered fast enough.

Since manual defragmentation just isn’t practical—who has time to defrag every system, every day – the best solution using Automatic defragmentation software such as Diskeeper, however if you looking for a free solution, you can use the VBS script below which can be scheduled using Windows XP/2k3 Task Scheduler. Copy and paste the text below into Notepad and save the file as “dfrg.vbs”:

‘************************************************************************************
‘ This script will execute a defrag session for each volume and output to a log file
‘ This script will be executed from a scheduled task item



‘ Dependancies: Requires Windows XP or .Net Server

‘*************************************************************************************
Option Explicit

‘Dimensionalize variables
dim drive, intRV,x

‘Dimensionalize Object Variables and Instantiate Objects
dim oWSH : set oWSH = CreateObject(“WScript.shell”)
dim oFSO : set oFSO = CreateObject(“Scripting.filesystemobject”)
dim oExec
dim oLogFile
dim oDrives

‘Set Constant Values
Const DriveTypeFixed = 2
Const ForWriting = 2

‘Initialize Log file
Set oLogFile = oFSO.OpenTextFile(“C:\Defrag.log”,ForWriting,True)

oLogFile.WriteLine “Initializing Defrag Routine at ” & FormatDateTime(Now(),0)

‘Get the number of drives on the system
Set oDrives = ofso.Drives

For each drive in oDrives
If drive.drivetype = DriveTypeFixed then
oLogFile.WriteLine “”
oLogFile.WriteLine “”
oLogFile.WriteLine “Begining Defrag of Drive ” & drive.path & ” at ” & FormatDateTime(Now(),0)
Set oExec = oWSH.Exec(“defrag.exe ” & drive.Path & ” /f /v”)
Do While oExec.Status = 0
WScript.Sleep 60000 ‘Sleep 1 minute
Loop
x=0
Do while Not oExec.StdOut.AtEndOfStream
oLogFile.Write(oExec.StdOut.Read(x))
x = x+1
Loop

End if
Next
oLogFile.WriteLine “”
oLogFile.WriteLine “Defrag Operation Complete at” & FormatDateTime(Now(),0)

Advertisements

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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: