Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  Advanced Search 


March 06, 2000

Scheduling Windows 2000’s Disk Defragmenter


RSS
Subscribe to Windows IT Pro | See More Windows 2000 Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

Download the Code Here

Contrary to what you might have heard, NTFS partitions in Windows 2000 (Win2K) and Windows NT do fragment over time. The system doesn't write files in contiguous areas on the hard disk. The larger the volume size, the more fragmented your hard disk is likely to become. As a result, it takes longer for the OS to access files and folders because it must perform extra disk reads to collect all the pieces. Even creating new files takes longer because the OS must locate free space scattered across the volume.

What Is Defragmentation?
Disk defragmentation is the process of reassembling files and folders in one location on a volume. The process, which works only on local volumes, consolidates files and folders in one contiguous place. Defragmentation results in improved disk access because it consolidates most—but not all—of the volume's free space. The time it takes to defragment a volume depends on several factors, including the size of the volume, the amount of fragmentation, the number of files and folders, and the available system resources. In Win2K, you can defragment all three types of supported file systems: FAT, FAT 32, and NTFS.

The Win2K Disk Defragmenter
Win2K includes a limited version of Executive Software's Disk Defragmenter. After logging on as an administrator, you can access the tool from Start, Programs, Accessories, System Tools, Disk Defragmenter (you can't execute the tool, dfrgntfs.exe, from the command prompt). Screen 1 shows the Disk Defragmenter console, which consists of two main areas: The upper area shows all the local volumes available on the disk, and the lower area shows how fragmented the highlighted volume is. The color legend at the bottom of the screen displays the fragmented, contiguous, and system files in red, blue, and green, respectively. The free space appears in white. The green areas, which appear only on NTFS volumes, represent the NTFS system files that you can’t move.

Analysis Reports
As Screen 2 shows, the analysis report gives you several pieces of useful information, including volume size, cluster size, and the amount of free and used space. You can also see information on volume, file, pagefile, directory (aka folder), and Master File Table (MFT) fragmentation. I find the average file size, listed under file fragmentation, particularly useful because it helps me determine an optimum cluster size for my volume. If the average file size is small, I’ll keep the cluster size small so I don't waste disk space.

A good indication of fragmentation is the average fragments per file, listed under the report's file fragmentation section. The optimum number is 1.00. If your fragments per file are 1.10, then about 10 percent of your files exist in two pieces. A value of 1.20 means 20 percent, and so on. If the fragments per file is 2.0, your files average two fragments each; 3.0 means three fragments each, and so on. The analysis report also shows you which files didn't defragment. You can print or save the analysis reports in a text file.

The Defragmentation Process
It's a good idea to analyze the volume before you start the defragmentation process. Doing so will not only let's you determine whether you need defragmentation, but also lets you compare before and after pictures so that you can see the improvement. As I mentioned earlier, Disk Defragmenter doesn’t consolidate all the free space on a volume; it moves these areas of free space into one location. You can't completely consolidate free space for several reasons: The pagefile is fragmented; NTFS reserves a portion of free space on NTFS partitions for the MFT; and partitions that contain many folders contribute to free space fragmentation. If the analysis report indicates that you need to defragment your volume, you can proceed with the defragmentation process. Screen 1 shows the analysis and the result of defragmentation in the graphical window on the bottom half of the console.

Defragmentation Tips
You can't defragment certain system files, including the pagefile and the MFT, because they're in use during normal Windows operations. One way to defragment a pagefile is to temporarily move it to a different volume. For example, to defragment the existing pagefile on your D: drive:

  1. Run the Disk Defragmenter tool to defrag the D: drive.
  2. Create a new pagefile on a different drive (e.g., the C: drive) and delete the one on the D: drive by setting its size to zero. Reboot your computer.
  3. Recreate the pagefile on the D: drive and delete the one on the C: drive by setting its size to zero.
  4. Reboot your computer one more time.

The system will create the new pagefile on the D: drive in a contiguous space, assuming you have enough contiguous disk space on the drive.

You should analyze your volume after deleting a large number of files or folders. For example, if you delete the I386 folder that contains Win2K source files or other large files such as video files, you should run Disk Defragmenter. You can only run one instance at a time. You should defragment file servers more often than desktop workstations because file servers frequently become fragmented.

Scheduling Disk Defragmenter
You should schedule file-server defragmentation during off-peak hours to minimize the effects on server performance. Unfortunately, Microsoft doesn’t provide an easy way to schedule disk defragmentation in Win2K. Because you can't execute dfrgntfs.exe from a command prompt, you can’t use a batch file or task scheduler to schedule the process. However, Listing 1, which you can download from Article Info box at the top of this page, contains a Visual Basic (VB) script that runs the tool at scheduled intervals. You can modify the script, which I've named dfrgntfs.vbs, to fit your needs. It starts the defragmentation process and closes the window when it's finished. Use the Scheduled Tasks tool, located in System Tools, to schedule this script. I’ve scheduled the script to run every Friday at 8:00 P.M., as Screen 3 shows. Make sure you select the Advanced tab to configure several additional options.

End of Article



Reader Comments
I have refined the script a little bit so it walks through all the drives:

'This script launches defrag and sends keys to the UI in order to automate the defrag
'process.

set WshShell = CreateObject("WScript.Shell")

'Launch Defrag from the command line and wait for a second
WshShell.Run "dfrg.msc"
WScript.Sleep 1000

'Wait until the application has loaded - Check every second
While WshShell.AppActivate("Disk Defragmenter") = FALSE
wscript.sleep 1000
Wend



'modifications Alfonso
Dim oFSO, oDrives, oDrive, firstjump
set oFSO=createobject("scripting.filesystemobject")
set oDrives=oFSO.Drives
firstjump=0
'We use this variable to check if we have jumped first in the drive list. It is necessary because the key sequence is a bit difference in the first jump
for each oDrive in oDrives

if odrive.drivetype=2 then

'Bring the application to the foreground
WshShell.AppActivate "Disk Defragmenter"
WScript.Sleep 200

'Send an ALT-A key to bring down the degrag menu
WshShell.SendKeys "%A"
WScript.Sleep 200

'Send a D to start the defrag
WshShell.SendKeys "D"

'Wait until the defrag is completed - Check for window every 5 seconds
While WshShell.AppActivate("Defragmentation Complete") = FALSE
wscript.sleep 5000
wend

'Bring the msgbox to the foreground
WshShell.AppActivate "Defragmentation Complete"
WScript.Sleep 200

'Send a tab key to move the focus from View Report button to the Close Button
WshShell.Sendkeys "{TAB}"
Wscript.Sleep 500

'Send key to Close the Defragmentation Complete window
WshShell.Sendkeys "{ENTER}"
Wscript.Sleep 500

'Bring the application to the foreground
WshShell.AppActivate "Disk Defragmenter"
WScript.Sleep 200

'Move down to next drive
if firstjump=0 then
WshShell.Sendkeys "{TAB}{DOWN}"
firstjump=1
else
WshShell.SendKeys"{DOWN}"
end if

end if
next

'Send and ALT-F4 to Close the Defrag program
WshShell.Sendkeys "%{F4}"

Alfonso Pinol March 08, 2000


It would be nice to be able to do a Run as on this command and be able to encrypt a password in it. The user workstations are secured so the user doesn't have admin rights to the workstation. We would like to have defrag available for them to execute and this would be a means. Please let me know if you have suggestions on how this can be accomplished.

Barb Kopitzke March 29, 2000


i would like to customize this script to cycle through all of my hard drives. the script that is provided only defrags the c drive, how do it get it to go through all local hard drives programatically?

mark badeau August 07, 2000


I have a question. the scripts i have found to run the defrag only do the drive attached to the main controller. none of them do the drive i have off an ultra 66 controller. my main partitions c,d,e are on the drive on this controller. is it possible to have a script that will do these drives.

Craig November 22, 2000


Check out http://kevin.gearhart.com/startdefrag

John Anderson February 06, 2001


I've updated Alfonso's code from below and added some code to exit the VB Script if it detects that the defrag app has closed.

<pre>

'This script launches defrag and sends keys to the UI in order to automate the defrag
'process.

set WshShell = CreateObject("WScript.Shell")

'Launch Defrag from the command line and wait for a second

WshShell.Run "dfrg.msc"
WScript.Sleep 1000

'Wait until the application has loaded - Check every second

While WshShell.AppActivate("Disk Defragmenter") = FALSE
wscript.sleep 1000
Wend

'modifications by Alfonsom, fixed/updated by Josh West

Dim fso, oDrives, oDrive, firstjump
set fso=CreateObject("Scripting.FileSystemObject")
set oDrives=fso.Drives
firstjump=0
'We use this variable to check if we have jumped first in the drive list. It is necessary because the key sequence is a bit difference in the first jump

for each oDrive in oDrives
if oDrive.DriveType=2 then
'Bring the application to the foreground
WshShell.AppActivate "Disk Defragmenter"
WScript.Sleep 200
'Send an ALT-A key to bring down the degrag menu
WshShell.SendKeys "%A"
WScript.Sleep 200
'Send a D to start the defrag
WshShell.SendKeys "D"
'Wait until the defrag is completed - Check for window every 5 seconds
While WshShell.AppActivate("Defragmentation Complete") = FALSE
if WshShell.AppActivate("Disk Defragmenter") = FALSE then
exit for
end if
wscript.sleep 5000
wend
'Bring the msgbox to the foreground
WshShell.AppActivate "Defragmentation Complete"
WScript.Sleep 200
'Send a tab key to move the focus from View Report button to the Close Button
WshShell.Sendkeys "{TAB}"
Wscript.Sleep 500
'Send key to Close the Defragmentation Complete window
WshShell.Sendkeys "{ENTER}"
Wscript.Sleep 500
'Bring the application to the foreground
WshShell.AppActivate "Disk Defragmenter"
WScript.Sleep 200
'Move down to next drive

if firstjump=0 then
WshShell.Sendkeys "{TAB}{DOWN}"
firstjump=1
else WshShell.SendKeys"{DOWN}"
end if
end if
next
'Send and ALT-F4 to Close the Defrag program

if WshShell.AppActivate("Disk Defragmenter") = TRUE then
WshShell.AppActivate "Disk Defragmenter"
WshShell.Sendkeys "%{F4}"
end if

</pre>

Josh West March 07, 2001


Hello Everyone,
I also have altered the code. I've attempted to clean it up a little, to make it a bit more readable.
I have also corrected the mistake made by Josh West(no offence Josh) when he altered Alfonso Pinol original code. The code will not run unless the DIM is set to oFSO, and not fso.
<br><br>
Now here is the problem I am having with the code. I have a total of 3 drives on my server, the first being a Compaq ROM FAT drive. Then a 2.38 Gigabyte NTFS C: drive, then a 6.04 Gigabyte NTFS D: drive. The script runs perfectly until it gets to the D: drive. At which point the script stops. It does not lockup it simply seems to be waiting. I can move between the other drives with the mouse pointer, and even manually continue the defrag on the D: drive.
<br><br>
Any ideas someone??
Thanks
<br><br>


'This script launches defrag and sends keys to the UI in
'order to automate the defrag process.
Set WshShell = CreateObject("WScript.Shell")

'Launch Defrag from the command line and wait for a second.
WshShell.Run "dfrg.msc"
WScript.Sleep 1000

'Wait until the application has loaded - Check every second.
While WshShell.AppActivate("Disk Defragmenter") = False
WScript.Sleep 1000

Wend

'Modifications by Alfonsom, fixed/updated by Josh West.
Dim oFSO, oDrives, oDrive, firstjump
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oDrives = oFSO.Drives
firstjump = 0

'We use this variable to check if we have jumped first in
'the drive list.
'It is necessary because the key sequence is a bit
'difference in the first jump.
For Each oDrive In oDrives
If oDrive.DriveType = 2 Then

'Bring the application to the foreground.
WshShell.AppActivate "Disk Defragmenter"
WScript.Sleep 200

'Send an ALT-A key to bring down the degrag menu.
WshShell.SendKeys "%A"
WScript.Sleep 200

'Send a D to start the defrag.
WshShell.SendKeys "D"

'Wait until the defrag is completed - Check for window
'every 5 seconds.
While WshShell.AppActivate("Defragmentation Complete") = False
If WshShell.AppActivate("Disk Defragmenter") = False Then
Exit For
End If
WScript.Sleep 5000

Wend

'Bring the msgbox to the foreground.
WshShell.AppActivate "Defragmentation Complete"
WScript.Sleep 200

'Send a tab key to move the focus from View Report button
'to the Close Button.
WshShell.SendKeys "{TAB}"
WScript.Sleep 500

'Send key to Close the Defragmentation Complete window.
WshShell.SendKeys "{ENTER}"
WScript.Sleep 500

'Bring the application to the foreground.
WshShell.AppActivate "Disk Defragmenter"
WScript.Sleep 200

'Move down to next drive
If firstjump = 0 Then WshShell.SendKeys "{TAB}{DOWN}"
firstjump = 1
Else: WshShell.SendKeys "{DOWN}"
End If
Next

'Send and ALT-F4 to Close the Defrag program.
If WshShell.AppActivate("Disk Defragmenter") = True Then
WshShell.AppActivate "Disk Defragmenter"
WshShell.SendKeys "%{F4}"
End If

'Send and ALT-F4 to Close the Defrag program.
If WshShell.AppActivate("Disk Defragmenter") = True Then
WshShell.AppActivate "Disk Defragmenter"
WshShell.SendKeys "%{F4}"
End If

Dave Fox May 31, 2001


Thanks for the script guys. It looks very promising so far. I've found the problem which Dave was looking for. There's a missing Sleep call after the next drive selection. I added a "WScript.Sleep 200" just between the two "end if"s before the "Next". By the way. There one of the "end if"s before the Next was missing in the script.

Mark Ayliffe July 23, 2001


AutoDeFrag is an open-source launcher for the standard defragmenter built into Windows 2000. The program and a link for the source code can be found at:

http://www.morphasys.com/autodefrag/

Larry Gilson October 01, 2001


A great piece of VB code !!! Thanks for sharing and not making me have to site down and debug my own stuff. Works like a charm on my Advanced Server 2000 !!! Thanks Again

Bill January 17, 2002


 See More Comments  1   2   3   4   5   6 

You must log on before posting a comment.

If you don't have a username & password, please register now.




Top Viewed ArticlesView all articles
VMware and the Future of Virtualization

What's next for virtualization and business IT? Windows IT Pro senior editor Jeff James speaks with VMware President and CEO Diane Greene on the future of virtualization technology. ...

Friday at PASS Europe 2006

Kevin talks about the closing day of the event and shares a funny Microsoft film. ...

The Memory-Optimization Hoax

Don't believe the hype. At best, RAM optimizers have no effect. At worst, they seriously degrade performance. ...


Windows OSs Whitepapers Why SaaS is the Right Solution for Log Management

Are You Satisfied?

A Preliminary Look at Deployment Plans for Microsoft Windows Vista

Related Events Check out our list of Free Email Newsletters!

Windows OSs eBooks Understanding and Leveraging Code Signing Technologies

A Guide to Windows Certification and Public Keys

SQL Server Administration for Oracle DBAs

Related Windows OSs Resources Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.

Job Openings in IT


ADS BY GOOGLE SPONSORED LINKS FEATURED LINKS

IT Connections
Dive into the new Microsoft platforms and products you implement and support with the experts from Microsoft, TechNet Magazine, Windows ITPro and industry gurus. There are 70+ sessions and interactive panels with networking opportunities.

Attention User Group Leaders...
Announcing the eNews Generator—a FREE HTML e-newsletter builder for user group leaders. Build your HTML and text e-newsletters in minutes and add Windows IT Pro & SQL Server Mag articles alongside your own message!.

Master SharePoint with 3 eLearning Seminars
Learn how to build a better SharePoint infrastructure and enable powerful collaboration with MVPs Dan Holme and Michael Noel. Register today!

Get SQL Server 2008 at WinConnections
Don’t miss Microsoft Exchange and Windows Connections conferences, the premier events for Microsoft IT Professionals in Las Vegas, November 10-13. Every attendee will receive a copy of SQL Server 2008 Standard Edition with one CAL.



Interested in Email Encryption?
Read about the advantages of identity-based encryption in this free report.

Order Your SQL Fundamentals CD Today!
Learn how to use SQL Server, understand Office integration techniques and dive into the essentials of SQL Express and Visual Basic with this free SQL Fundamentals CD.

Virtualization Congress Oct. 14-16 in London
Don't miss Virtualization Congress, the premiere EMEA conference dedicated to hardware, OS and application virtualization. Oct. 14-16.
Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro Windows Dev Pro IT Job Hound ITTV
IT Library Technical Resources Directory Connected Home Windows Excavator Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2008 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing