Recipe 3.10. Checking a Volume for ErrorsProblemYou want to check a volume for errors. SolutionUsing a graphical user interface
Using a command-line interfaceThe chkdsk utility can detect problems with a volume and attempt to fix them. Specify the name of the volume you want to check to run chkdsk in read-only mode: > chkdsk D: Use the /f option to have chkdsk attempt to fix any errors it finds: > chkdsk D: /f With the /f option, chkdsk will try to lock the drive, so if it is in use by another process, you will only be able to schedule it to run during the next reboot. You can include the /x option with /f to force the volume to be dismounted (for a nonsystem volume). Using VBScript' This code tries to perform a chkdsk on the specified volume. ' ------ SCRIPT CONFIGURATION ------ strComputer = "." strDrive = "<Drive>" ' e.g., D: boolFixErrors = True ' True = chkdsk /f, False = chkdsk ' ------ END CONFIGURATION --------- set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") set objDisk = objWMI.Get("Win32_LogicalDisk.DeviceID='" & strDrive & "'") intRC = objDisk.ChkDsk(boolFixErrors) if intRC = 0 then WScript.Echo "Chkdsk completed successfully." elseif intRC = 1 then WScript.Echo "Chkdsk scheduled on next reboot." else WScript.Echo "Error running chkdsk: " & intRC end if DiscussionOn Windows Server 2003, you can use the new vrfydsk command to perform the same function as chkdsk in read-only mode. Vrfydsk creates a shadow copy of the target volume, assigns a drive letter to it, runs chkdsk (read-only mode) against that drive, unassigns the drive, and removes the shadow copy. This is useful because often when running chkdsk on an active volume it may report transient errors that are due to the volume being in use. With vrfydsk, you don't have to worry about disk activity skewing the results. If vrfydsk reports that errors were found, you'll want to schedule a chkdsk /f as soon as possible on the volume. See AlsoMSDN: Chkdsk Method of the Win32_Volume Class, MS KB 160963 (CHKNTFS.EXE: What You Can Use It For), MS KB 187941 (An explanation of CHKDSK and the new /C and /I switches), MS KB 191603 (Modifying the Autochk.exe Time-out Value), MS KB 218461 (Description of Enhanced Chkdsk, Autochk, and Chkntfs Tools in Windows 2000), and MS KB 837326 (How to use the Vrfydsk.exe tool to check a volume for errors without taking the volume offline in Windows Server 2003). |