I'm looking for a way to check the returncode / errorlevel of the SC command in a DOS script. How can I get this information ?

link|improve this question
feedback

2 Answers

In most cases you can just check the value of %errorlevel%.

echo %errorlevel%

However, your comment seems to be correct that the %errorlevel% is useless for sc in particular. I'll keep the links included below for people who still want to read about %errorlevel% and application exit codes, but it appears I have not answered your question.


Application Exit Codes - http://stackoverflow.com/questions/334879/how-do-i-get-the-application-exit-code-from-a-windows-command-line

This is an explanation of exactly what errorlevel is - http://blogs.msdn.com/oldnewthing/archive/2008/09/26/8965755.aspx

link|improve this answer
1  
if you try the errorlevel with the SC command you will find out that it doesn't work. Why the SC command is so special in this case I don't know. working with net start / net stop the %errorlevel% is reliable. (I'm aware of the speciality if this old fellow and use it accordingly). – nojetlag Mar 23 '11 at 16:08
feedback

I suspect you are mostly after the information whether the command succeeded or not. In that case you can simply use

if not errorlevel 1 ...

which will execute the ... part if the last exit code was 0. (It gets a little more complicated than that, but you can ignore that for the most part).

Checking the pseudo-variable %errorlevel% can be dangerous because if someone set a variable with that name before, its value will overshadow the pseudo-variable expansion.

link|improve this answer
thanks, aware of this, however as mentioned above, SC command is not playing nicely with errorlevel. – nojetlag Mar 23 '11 at 16:08
feedback

Your Answer

 
or
required, but never shown

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