Home > Windows Server Tips > Windows Systems Management and Administration > Defrag script for all volumes
Windows Server Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

WINDOWS SYSTEMS MANAGEMENT AND ADMINISTRATION

Defrag script for all volumes


Michael McCamey
09.20.2006
Rating: -4.67- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


There has been much discussion about automating disk defragmentation now that the command line can be used. Most scripts just run these commands for a single volume. Many administrators have several volumes; typically for the OS and for data. Below is a script that finds all local volumes and runs the defrag for each volume.

This script works for Server 2003 and XP.

Code: Option Explicit

Dim WshShell, fso, d, dc, ErrStr(), Return, X, A(), MyFile, I, MyBox, Drive

Set WshShell = WScript.CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
X = 0

Set dc = fso.Drives
For Each d in DC
If d.DriveType = 2 Then
X = X + 1

'Determine drive letter of first fixed disk
'This is the drive that the error report will be placed on
If X = 1 Then
Drive = d
End If
End If
Next

ReDim A(X)
ReDim ErrStr(X)

X = 0
For Each d in dc
If d.DriveType = 2 Then
X = X + 1
Return = WshShell.Run("defrag " & d & " -f -v", 1, TRUE)

'Determine the Error code returned by Defrag for the current drive and save it
If return = 0 then
ErrStr(x) = ErrStr(x) & "Drive " & d & " Defrag completed successfully" & vbCRLF
elseif return = 1 then
ErrStr(x) = ErrStr(x) & "Drive " & d & " Defrag aborted with error level " & return & " 
(defrag was cancelled manually) " & vbCRLF
elseif return = 2 then
ErrStr(x) = ErrStr(x) & "Drive " & d & " Defrag aborted with error level " & return & "
 (there was a command line error. Check your command line for valid switches and 
drives)" & vbCRLF
elseif return = 3 then
ErrStr(x) = ErrStr(x) & "Drive " & d & " Defrag aborted with error level " & return & " 
(there was an unknown error)" & vbCRLF
elseif return = 4 then
ErrStr(x) = ErrStr(x) & "Drive " & d & " Defrag aborted with error level " & return & " 
(defrag could not run due to insufficient memory resources)" & vbCRLF
'errorlevel 5 is not currently used
elseif return = 5 then
ErrStr(x) = ErrStr(x) & "Drive " & d & " Defrag aborted with error level " & return & " 
(general error)" & vbCRLF
elseif return = 6 then
ErrStr(x) = ErrStr(x) & "Drive " & d & " Defrag aborted with error level " & return & 
"(System error: either the account used to run defrag is not an administrator, 
there is a problem loading the resource DLL, or a defrag engine could not be accessed. 
Check for proper user permissions and run Sfc.exe to validate system files)" & vbCRLF
elseif return = 7 then
ErrStr(x) = ErrStr(x) & "Drive " & d & " Defrag aborted with error level " & return & " 
(There is not enough free space on the drive. Defrag needs 15% free space to run 
on a volume)"
 & vbCRLF
else
ErrStr(x) = ErrStr(x) & "Drive " & d & " Defrag aborted with an unknown error level: " 
& return & vbCRLF
end if

End If
Next

'Create the Error Report in the root of the first fixed disk.
Set MyFile = fso.OpenTextFile(Drive & "defrag.txt", 2, True)
MyFile.WriteLine(Date) & vbCRLF
MyFile.WriteLine(Time) & vbCRLF
For I = 1 to X
MyFile.WriteLine(ErrStr(I))
Next
MyFile.Close


Set WshShell = Nothing
Set fso = Nothing

Please let us know how useful you find this tip by rating it below! If you have a useful Windows tip, timesaver or workaround to share, submit it to our tip contest and you could win a prize!

Rate this Tip
To rate tips, you must be a member of SearchWindowsServer.com.
Register now to start rating these tips. Log in if you are already a member.


Submit a Tip




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


RELATED CONTENT
Windows Management Instrumentation (WMI) Scripting
Windows scripting secrets for disk quota management
Windows scripting tutorials for systems administrators
Filtering output from Windows PowerShell
Working with WMI providers to PowerShell
Vista's Windows kill switch: What to do if RFM kicks in
Scripting School: Why you should care about Windows PowerShell
Christa Anderson's Scripting School for Windows administrators
Microsoft tool exposes WMI namespaces and properties
Microsoft utility helps admins diagnose WMI services
Scripting School: Enhancing scripts that require user input

Microsoft Windows Server 2003 Administration
How to install Windows Server 2003 patches when offline
Validating Windows server clusters with ClusPrep
Exploring the Windows Server 2003 Resource Kit: Clusfileport.dll
Exploring the Windows Server 2003 Resource Kit: Cmdhere.inf and CMGetCer.inf
Windows server security management: Security expert roundup
Windows server management with Remote Desktop
Avoid DC restoration problems with authoritative restore
Exploring the Windows Server 2003 Resource Kit: Confdisk.exe
Exploring the Windows Server 2003 Resource Kit: Compress.exe and Expand.exe
Exploring the Windows Server 2003 Resource Kit: Clusterrecovery.exe
Microsoft Windows Server 2003 Administration Research

Windows Systems Management and Administration
Tips for Windows domain controller optimization
Quick hits: Troubleshooting service account failure, batch job execution
Case Study: Troubleshooting Windows service dependency failures
Troubleshooting common Windows service failures
How to format NTFS: More tricks to improve file system performance
Key enhancements to SCCM give admins more control over assets, licensing
Windows scripting secrets for disk quota management
Optimizing NTFS file system performance
The new Microsoft System Center: What happened to SMS and MOM?
New Russinovich tool scans for open file references from command line

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.

HomeTopicsITKnowledge ExchangeTipsAsk the ExpertsMultimediaWhite PapersIT Downloads
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 2004 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts