Page 1 of 1

Question about performing "chkdsk" from batch at next reboot

Posted: 20 Feb 2015 00:48
by pstein
Occasionally I want to perform a well know "chkdsk" command for my Win 7 system at next reboot.
Since I do not always remember immediately the exact syntax I put the following command into a DOS batch script:

chkdsk C: /F /V /R /X >D:\tmp\chkdskreport.log

Mind the /X which means "perform at next reboot"!

When I doubleclick on the batch file it is started BUT from the command prompt the processing stops somehow.
A closer look into the log file reveals: chkdsk is informing the user that the partition C: is locked (=by Win 7 system) andasks if it should be performed at next reboot.

Hmm, why I am asked again? I passed already the /X flag which means YES DO IT at next reboot.
Secondly: How can I view at COMMAND PROMPT window the question (and not only in logfile)?

How can I automatically pass a "y" (without quotes) to the command?

Finally: When I manually enter "y" +<return> at command prompt then yes, the chkdsk is processed at next startup. But no output is not written into file D:\tmp\chkdskreport.log

Why?

Peter

Re: Question about performing "chkdsk" from batch at next re

Posted: 20 Feb 2015 03:04
by ShadowThief
You can pass a y to a command like this:

Code: Select all

echo y|chkdisk: /F /V /R /X >D:\tmp\chkdskreport.log

Re: Question about performing "chkdsk" from batch at next re

Posted: 20 Feb 2015 04:49
by foxidrive
pstein wrote:I passed already the /X flag which means YES DO IT at next reboot.
Why?


That's not exactly right - it means to dismount the volume.
Say you have a drive H: that you use this switch on, then it will dismount the volume and close any open files
and go ahead.

But the system drive cannot be dismounted - so it has to perform a scheduled chkdsk and asks you for confirmation.

Re: Question about performing "chkdsk" from batch at next re

Posted: 20 Feb 2015 07:50
by Squashman
pstein wrote:Mind the /X which means "perform at next reboot"!

That is not what the documentation says.

To run chkdsk at boot up you would do this.
Run at Bootup

Running at bootup is often the easiest way to close all open file handles.

Use chkdsk, chkntfs or the FSUTIL dirty commands to set or query the volumes 'dirty' bit so that Windows will run chkdsk when the computer is restarted. This setting is also found in the BootExecute value under HKLM\System\CurrentControlSet\Control\Session Manager

Re: Question about performing "chkdsk" from batch at next re

Posted: 10 Mar 2015 01:13
by pstein
Thank you.
But I still don't understand.

Ok /X is only for dismounting.

But assume I want to chkdsk drive D:\ at next reboot (even if I could dismount it in current session).

How can I achieve this?

Peter

Re: Question about performing "chkdsk" from batch at next re

Posted: 10 Mar 2015 03:13
by ShadowThief
Personally, I'd just create a task in Task Scheduler, set it to run "When the computer starts" and then point the "Start a program" option at the script you wrote.

Re: Question about performing "chkdsk" from batch at next re

Posted: 10 Mar 2015 03:50
by pstein
Ok.

Can I create a task from DOS batch script (as cmdline) or do I have to click through multiple menus and dialoges and buttons and settings :-(

Re: Question about performing "chkdsk" from batch at next re

Posted: 10 Mar 2015 04:02
by OperatorGK
Use schtasks /Create /SC ONSTART /TN [name] /TR [your command here] once to do your command at every boot.

Re: Question about performing "chkdsk" from batch at next re

Posted: 10 Mar 2015 07:18
by Compo
You could also add the request directly to the registry and reboot.

The following example requests a check of your system drive:

Code: Select all

@Echo Off
SetLocal
Set "_Key=HKLM\SYSTEM\CurrentControlSet\Control\Session Manager"
Set "_Val=BootExecute"
Set "_New=autocheck autochk /r \??\%SystemDrive%\0"
For /F "Tokens=2*" %%A In ('Reg Query "%_Key%" /V "%_Val%"') Do Set "_Xst=%%B"
Reg Add "%_Key%" /V "%_Val%" /T REG_MULTI_SZ /D "%_New%%_Xst%" /F>Nul
ShutDown /r /d P:1:1
I haven't included any logging etc. since that is added to the event viewer anyway.

Re: Question about performing "chkdsk" from batch at next re

Posted: 13 Mar 2015 11:27
by Samir
Another method would be to check for a particular file at startup (that you set up) and if this file exists (or doesn't), run a chkdsk.

For example:

Code: Select all

IF EXIST C:\CHKREPRT.TXT GOTO END
CHKDSK C: > CHKREPRT.TXT
:END
In this example, all you would have to do is delete C:\CHKREPRT.TXT (ideally after reviewing it), and the next time the batch is run (like in the startup or autoexec.bat if win7 has one), the chkdsk will run again.

Re: Question about performing "chkdsk" from batch at next re

Posted: 13 Mar 2015 11:35
by Squashman
Samir wrote:Another method would be to check for a particular file at startup (that you set up) and if this file exists (or doesn't), run a chkdsk.

Nope. He wants to check the system volume. That has to be done before Windows starts!

Re: Question about performing "chkdsk" from batch at next re

Posted: 13 Mar 2015 14:06
by Samir
Squashman wrote:
Samir wrote:Another method would be to check for a particular file at startup (that you set up) and if this file exists (or doesn't), run a chkdsk.

Nope. He wants to check the system volume. That has to be done before Windows starts!
Is there not a way to run commands before 'windows' starts like in 95/98?