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:
- Run the Disk Defragmenter tool to defrag the D: drive.
- 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.
- Recreate the pagefile on the D: drive and delete the one on the C: drive by setting its size to zero.
- 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.
'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