Long island web design, hosting company
Home About Us Support Products Services Portfolio Contact Us Home

Windows 2K/XP > Batch file: Automate Chkdsk and Defrag on WinXP machines

Created On: 11/29/2004 4:45:00 PM

This batch file can save you some time by automatically running Chkdsk and Defrag on your Windows XP machines. First, it does a read-only Chkdsk. If Chkdsk sets the errorlevel to 3, meaning that it found errors and needs to be run with the /F (fix) option, the file checks to see whether the systemroot or pagefile exist on the drive. If so, it runs Chkdsk the next time the system is rebooted. If systemroot or pagefile don't exist on the drive, it runs a Chkdsk /F /R now, and then it runs a Defrag -b and a regular Defrag. (The -b switch is an undocumented switch that optimizes the drive for boot.)

Using the file

To put the file to work, follow these steps:

            Copy the code in Listing A.

            Open Notepad and paste in the code.

            Save the file as Dskchk.cmd.

            Create DrvLtr.txt and list the letters of the drives you want checked, with each letter on its own line. Under the last drive letter, type the word end. For example:

C:

F:

end

            Put Dskchk.cmd and DrvLtr.txt in the same directory. You can then schedule Dskchk with Task Scheduler or run it manually.

Listing A

REM chkdsk and defrag automation

for /F "eol= tokens=1 delims=( " %%i in (DrvLtr.txt) do set DrvLtr=%%i& call :dsKchk
 
:dsKchk
If %DrvLtr% == end goto :eof
chkdsk %DrvLtr%
If not errorlevel 3 goto :defrag
If not exist %DrvLtr%\winnt If not exist %DrvLtr%\windows If not exist %DrvLtr%\pagefile.sys goto :dskchkon
 
:dskchkoff
cd\
%DrvLtr%
echo Y chkdsk /F /R
goto :defrag
 
:dskchkon
chkdsk %DrvLtr% /F /R
 
:defrag
cd\
%DrvLtr%
defrag %DrvLtr% -b
defrag %DrvLtr%
:EOF