0

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 ?

pk.
  • 6,451
  • 2
  • 42
  • 63
nojetlag
  • 101
  • 1
  • 3

2 Answers2

3

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 - https://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

pk.
  • 6,451
  • 2
  • 42
  • 63
  • 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
  • You can always obtain the errorlevel; it's virtually impossible for a program not to have one. But it is very well possible that the errorlevel is always 0. Having non-zero errorlevels is special. – MSalters Nov 27 '14 at 15:13
0

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.

Joey
  • 1,853
  • 11
  • 13
  • thanks, aware of this, however as mentioned above, SC command is not playing nicely with errorlevel. – nojetlag Mar 23 '11 at 16:08