Batch file to run Check Disk

  • Thread starter Thread starter G Lamar
  • Start date Start date
G

G Lamar

Anyone have a batch file that can run CHKDSK /F from a command (cmd)
prompt in Windows XP on, say, ~6 HDs consecutively? Would appreciate
it if you would paste it into a response. Thanks.
--
 
Hi,

Yes, it can be done, but its not practical. Often a drive is locked, and
the /F switch cannot be used without dismounting the volume, or waiting
until the next reboot.

If user intervention is required to answer a prompt, then the BAT file or
script will pause, waiting for the user to enter a Y or N.

If you can live with this, then the following code, copied and pasted into a
Notepad file and saved with a VBS extension will accomplish what you want.

Set WshShell = WScript.CreateObject("WScript.Shell")

Dim fso, d, dc
Set fso = CreateObject("Scripting.FileSystemObject")
Set dc = fso.Drives
For Each d in dc
If d.DriveType = 2 Then
Return = WshShell.Run("chkdsk " & d & " /f", 1, TRUE)
End If
Next

Set WshShell = Nothing
 
Thanks Kelly, the Y & N is fine. However, the vb script doesn't seem
to work. It loops without running chkdsk and creates a lot of
windows?
--


Kelly said:
Hi,

Yes, it can be done, but its not practical. Often a drive is locked, and
the /F switch cannot be used without dismounting the volume, or waiting
until the next reboot.

If user intervention is required to answer a prompt, then the BAT file or
script will pause, waiting for the user to enter a Y or N.

If you can live with this, then the following code, copied and pasted into a
Notepad file and saved with a VBS extension will accomplish what you want.

Set WshShell = WScript.CreateObject("WScript.Shell")

Dim fso, d, dc
Set fso = CreateObject("Scripting.FileSystemObject")
Set dc = fso.Drives
For Each d in dc
If d.DriveType = 2 Then
Return = WshShell.Run("chkdsk " & d & " /f", 1, TRUE)
End If
Next

Set WshShell = Nothing
 
It works here so describe exactly what happens.

--
----------------------------------------------------------
http://www.g2mil.com/Dec2003.htm
G Lamar said:
Thanks Kelly, the Y & N is fine. However, the vb script doesn't seem
to work. It loops without running chkdsk and creates a lot of
windows?
--


Kelly said:
Hi,

Yes, it can be done, but its not practical. Often a drive is locked, and
the /F switch cannot be used without dismounting the volume, or waiting
until the next reboot.

If user intervention is required to answer a prompt, then the BAT file or
script will pause, waiting for the user to enter a Y or N.

If you can live with this, then the following code, copied and pasted into a
Notepad file and saved with a VBS extension will accomplish what you want.

Set WshShell = WScript.CreateObject("WScript.Shell")

Dim fso, d, dc
Set fso = CreateObject("Scripting.FileSystemObject")
Set dc = fso.Drives
For Each d in dc
If d.DriveType = 2 Then
Return = WshShell.Run("chkdsk " & d & " /f", 1, TRUE)
End If
Next

Set WshShell = Nothing

--
All the Best,
Kelly
<TYDB>

MS-MVP Win98/XP
[AE-Windows® XP]

Troubleshooting Windows XP
http://www.kellys-korner-xp.com

Utilities for Windows XP
http://www.kellys-korner-xp.com/xp_u.htm#xp_util


G Lamar said:
Anyone have a batch file that can run CHKDSK /F from a command (cmd)
prompt in Windows XP on, say, ~6 HDs consecutively? Would appreciate
it if you would paste it into a response. Thanks.
 
G said:
Anyone have a batch file that can run CHKDSK /F from a command (cmd)
prompt in Windows XP on, say, ~6 HDs consecutively? Would appreciate
it if you would paste it into a response. Thanks.

Hi

Here is a vbscript version that will run CHKDSK without using /F on all drives
and create a log file (if you add /F the script will just hang so you
shouldn't)...


'chkdsk_all.vbs
'Runs chkdsk on all hard disks - Can be run as a Scheduled Task
'puts the output of chkdsk in a log and display it
'© Torgeir Bakken - 2002-03-05
'Based on the script defrag_all2.vbs by Doug Knox
'This code may be freely distributed/modified

Option Explicit

Dim WshShell, fso, d, dc, LogFile, Drive

Set WshShell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")

Set dc = fso.Drives
For Each d in DC
'Determine drive letter of first fixed disk
'This is the drive that the report will be placed on
If d.DriveType = 2 Then
Drive = d
Exit For
End If
Next

LogFile = Drive & "\chkdskreport.txt"

WshShell.Run "%comspec% /c echo Starting chkdsk of all hard disks " _
& Now & " >" & LogFile, 0, True
For Each d in dc
If d.DriveType = 2 Then
WshShell.Run "%comspec% /c echo ********* Checking drive " & d _
& " ******** >>" & LogFile, 0, True
WshShell.Run "%comspec% /c chkdsk.exe " & d & " >>" & LogFile, 0, True
End If
Next

WshShell.Run LogFile
 
G said:
Thanks Kelly, the Y & N is fine. However, the vb script doesn't seem
to work. It loops without running chkdsk and creates a lot of
windows?

Hi

I can bet you have created a vbscript or batch file with the name chkdsk.
Never give a script file the same name as an OS command, it often end up
in a loop like you have experienced (been there, done that ;-).

Also, the script could be changed a bit to avoid this issue even if you
have named your script chkdsk.bat/chkdsk.vbs:

Instead of
Return = WshShell.Run("chkdsk " & d & " /f", 1, TRUE)

you should use
Return = WshShell.Run("chkdsk.exe " & d & " /f", 1, TRUE)

specifying the file extension to .exe for the chkdsk command to be called.


As a side not, as the script is not using the return code in the 'Return'
variable, you could use this line as well:

WshShell.Run "chkdsk.exe " & d & " /f", 1, TRUE
 
Torgeir Bakken (MVP) said:
Hi

I can bet you have created a vbscript or batch file with the name chkdsk.
Never give a script file the same name as an OS command, it often end up
in a loop like you have experienced (been there, done that ;-).

Also, the script could be changed a bit to avoid this issue even if you
have named your script chkdsk.bat/chkdsk.vbs:

Instead of
Return = WshShell.Run("chkdsk " & d & " /f", 1, TRUE)

you should use
Return = WshShell.Run("chkdsk.exe " & d & " /f", 1, TRUE)

specifying the file extension to .exe for the chkdsk command to be called.


As a side not, as the script is not using the return code in the 'Return'
variable, you could use this line as well:

WshShell.Run "chkdsk.exe " & d & " /f", 1, TRUE


--
torgeir
Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of the 1328 page
Scripting Guide: http://www.microsoft.com/technet/scriptcenter


You got it - thanks!
--
 
Torgeir Bakken (MVP) said:
Hi

Here is a vbscript version that will run CHKDSK without using /F on all drives
and create a log file (if you add /F the script will just hang so you
shouldn't)...


'chkdsk_all.vbs
'Runs chkdsk on all hard disks - Can be run as a Scheduled Task
'puts the output of chkdsk in a log and display it
'© Torgeir Bakken - 2002-03-05
'Based on the script defrag_all2.vbs by Doug Knox
'This code may be freely distributed/modified

Option Explicit

Dim WshShell, fso, d, dc, LogFile, Drive

Set WshShell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")

Set dc = fso.Drives
For Each d in DC
'Determine drive letter of first fixed disk
'This is the drive that the report will be placed on
If d.DriveType = 2 Then
Drive = d
Exit For
End If
Next

LogFile = Drive & "\chkdskreport.txt"

WshShell.Run "%comspec% /c echo Starting chkdsk of all hard disks " _
& Now & " >" & LogFile, 0, True
For Each d in dc
If d.DriveType = 2 Then
WshShell.Run "%comspec% /c echo ********* Checking drive " & d _
& " ******** >>" & LogFile, 0, True
WshShell.Run "%comspec% /c chkdsk.exe " & d & " >>" & LogFile, 0, True
End If
Next

WshShell.Run LogFile



--
torgeir
Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of the 1328 page
Scripting Guide: http://www.microsoft.com/technet/scriptcenter

Outstanding - thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top