3.8. Defragmenting a Volume

Problem

You want to defragment a volume to improve disk access performance.

Solution

Using a graphical user interface

  1. Open Windows Explorer.

  2. Right-click the drive you want to defragment and select Properties.

  3. Click the Tools tab.

  4. Click the Defragment Now button. This launches the Disk Defragmenter application.

  5. Click the Analyze button to find out how badly the volume is fragmented. After the analysis is complete, Windows will inform you whether it believes you should defragment the volume.

  6. Click the View Report button to view statistics about fragmentation and to see the most fragmented files.

  7. Click the Defragment button to proceed with defragmenting the volume.

Using a command-line interface

The defrag utility is the command-line version of the Disk Defragmenter application (available on Windows XP and Windows Server 2003). Run the following command to perform an analysis of the D: drive:

> defrag d: /a

Add the /v option to see similar information to the View Report button in Disk Defragmenter:

> defrag d: /a /v

Finally, include the drive and /v (for verbose output) to perform a defragmentation of the volume:

> defrag d: /v

You can force a defragmentation even if disk space is low by including the /f option.

Using VBScript

' This code simulates the 'defrag /a /v' command except it analyzes ' all fixed disks, not just a specific one. ' The Win32_Volume class is new in Windows Server 2003 ' ------ SCRIPT CONFIGURATION ------ strComputer = "." ' ------ END CONFIGURATION ...

Get Windows Server Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.