0

I run maintenance for a lot of offices and use chkdsk as a part of the maintenance. I've recently been using PSEXEC to start a batch file that simply runs chkdsk and then pauses to keep the command prompt window open so that I can check to see if there were any errors found.

I would like to automate the process a little more.

I read somewhere (sorry my browser is not letting me cut and paste the link to the TechNet article) that chkdsk has exit codes that can be used to determine if any errors were found or not.

My problem is that I do not know where to begin in making use of these error codes? Could anyone shed some light or point me in the right direction on what I need to learn so that I can make a batch file or script that can run the chkdsk and then perform another action depending on the exit code?

Mircea Vutcovici
  • 17,619
  • 4
  • 56
  • 83
Charles
  • 21
  • 1
  • 4

2 Answers2

1

The batch file is interpreted by CMD. The exit status of a command is stored in %errorlevel% variable.

echo %ERRORLEVEL%

or:

goto error_%ERRORLEVEL%
:error_0
echo Ok
:error_1
echo Exit code 1.
...

The if command in cmd can test for the status of the last command:

IF ERRORLEVEL 0 goto okay
echo Error.
goto end
:okay
echo Normal exit.
:end

For details run inside cmd:

 help if
Mircea Vutcovici
  • 17,619
  • 4
  • 56
  • 83
  • The possible exit codes for ChkDsk is documented on [TechNet](http://technet.microsoft.com/en-us/library/cc730714(v=ws.10).aspx). '0: No errors were found. 1: Errors were found and fixed. 2: Performed disk cleanup (such as garbage collection) or did not perform cleanup because /f was not specified. 3: Could not check the disk, errors could not be fixed, or errors were not fixed because /f was not specified.' – abstrask May 09 '13 at 20:04
  • To add, you can also pipe the output using chkdsk > C:\chkdsklog.log first – TheCleaner May 09 '13 at 20:44
-1

ok here's a link to do this via powershell which I'd think would be more robust then a psexec solution (not that psexec is bad).

http://msdn.microsoft.com/en-us/library/windows/desktop/aa384915(v=vs.85).aspx

tony roth
  • 3,884
  • 18
  • 14
  • 1
    Whilst this may theoretically answer the question, [it would be preferable](http://meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – EEAA May 09 '13 at 20:56