Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

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
share|improve this answer
    
The possible exit codes for ChkDsk is documented on TechNet. '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 9 '13 at 20:04
    
To add, you can also pipe the output using chkdsk > C:\chkdsklog.log first –  TheCleaner May 9 '13 at 20:44

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

share|improve this answer
1  
Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference. –  EEAA May 9 '13 at 20:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.