Link to home
Start Free TrialLog in
Avatar of sbornstein2
sbornstein2

asked on

Command Line Extensions - ErrorLevel?

Hello all,

I am trying to test something on my machine.  I want to use ERRORLEVEL instead of %ERRORLEVEL% so using a SETLOCAL and then echo the error but everytime I run a echo ERRORLEVEL it returns "ERRORLEVEL" string vs an actual error number like 0.  I looked in the registry both local machine and user and the EnableExtensions is set to 1.  

Any idea?   I am trying to troubleshoot remote why a DTEXEC batch file call which is checking ERRORLEVEL is not returning a failure and I am wondering if it is because it is taking ERRORLEVEL as as string echoed.
Avatar of Bill Prew
Bill Prew

ERRORLEVEL is not an environment variable like %ERRORLEVEL% is, so you have to test it using an IF statement.  See IF /? at a command prompt for a bit more info.

~bp
A bit more info here as well.

http://www.robvanderwoude.com/errorlevel.php

~bp
Correct. ERRORLEVEL can only be used in IF, while %ERRORLEVEL% (or !ERRORLEVEL!, if Delayed Expansion is enabled) is available whenever other environment vars are.
What is the current code lines doing btw, and waht is working?  I presume you are aware that when you check the errorlevel using "if errorlevel" that is checks whether the error number is what you check or above, i.e.

if errorlevel 1 echo ERROR

shows ERROR if the errorlevel of previous command was 1 or more.
Steve
Avatar of sbornstein2

ASKER

@ECHO OFF
SETLOCAL EnableDelayedExpansion

:LOOP
REM do stuff

IF ERRORLEVEL == 6 GOTO END
IF ERRORLEVEL == 5 GOTO END
IF ERRORLEVEL == 4 GOTO END
IF ERRORLEVEL == 3 GOTO END
IF ERRORLEVEL == 1 GOTO END

IF ERRORLEVEL == 0 GOTO LOOP

:END
ECHO Stopped

ENDLOCAL
There may not be a problem with what I have and something else with a scheduler program I am using.  What I am going to test next is this and pass a exit statement and 1 instead.  Just want to verify anything I may need to check on the server that ERRORLEVEL is not getting populated:

: Run Start Up Package with FolderName and ServerName variables
"%DTEXEC%" /SQL "%PackagePath%\Start Up" /SERVER "%SERVERNAME_SSIS%"

REM - SB added new exit code detail
IF NOT ERRORLEVEL 0 GOTO END

IF ERRORLEVEL == 0 GOTO LOOP

:END
EXIT /B 1
==> IF ERRORLEVEL == 0 GOTO LOOP

This is not valid, you can't test the special value ERRORLEVEL against a value like that, you can only say

IF ERRORLEVEL 0 GOTO LOOP

or

IF NOT ERRORLEVEL 0 GOTO LOOP

The syntax is that the current value of ERRORLEVEL most recently set is compared to the number you specify.  If ERRORLEVEL is equal or greater it executes the statement, in this case the got. So if we say:

IF ERRORLEVEL 0 GOTO LOOP

Then we go to LOOP only when ERRORLEVEL is greater than or equal to 0.  Naturally we only get to the statement following this if ERRORLEVEL was less than 0.

On the other hand if we say:

IF NOT ERRORLEVEL 0 GOTO LOOP

Then we got to LOOP when ERRORLEVEL is less than 0, and only get to the next instruction after this one if ERRORLEVEL is greater than or equal to 0.

~bp
I also seem to vaguely remember some issues with EXIT /B and the task scheduler, and that you had to use EXIT n without the /B, but not positive.  Steve, any recollection of that?

~bp
Some discussion of the EXIT /B issue on earlier versions of the task scheduler here:

https://www.experts-exchange.com/questions/26968585/batch-file-that-checks-the-return-code-of-executables-it-runs.html

~bp
so then maybe the only issue with the original file is this:
IF ERRORLEVEL == 6 GOTO END
IF ERRORLEVEL == 5 GOTO END
IF ERRORLEVEL == 4 GOTO END
IF ERRORLEVEL == 3 GOTO END
IF ERRORLEVEL == 1 GOTO END

IF ERRORLEVEL == 0 GOTO LOOP

should be:
IF ERRORLEVEL 6 GOTO END
IF ERRORLEVEL 5 GOTO END
IF ERRORLEVEL 4 GOTO END
IF ERRORLEVEL 3 GOTO END
IF ERRORLEVEL 1 GOTO END

IF ERRORLEVEL 0 GOTO LOOP

correct?  I think it may have worked before but cant remember

ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You only need two commands:

IF ERRORLEVEL 1 GOTO END
goto LOOP

As explained, IF ERRORLEVEL checks for a value greater or equal, so the 1 would catch all other errors, too. As long as you do not need special treatment based on the error code, you can use that simplified version.
Qlemo I don't think in the case of running a DTEXEC call that it is always 1:

:: dtexec error codes:
:: (0) The package executed successfully.
:: (1) The package failed.
:: (3) The package was canceled by the user.
:: (4) The utility was unable to locate the requested package. The package could not be found.
:: (5) The utility was unable to load the requested package. The package could not be loaded.
:: (6) The utility encountered an internal error of syntactic or semantic errors in the command line.
Qlemo's point wasn't that it was always 1, but since you are always going to END for any value greater than or equal to 1, then you can do that with a single IF rather then several as you had.  That's all he was seeing as an optimization of the code.

~bp
Sorry was out today and office internet connection gone dead so no email server :-(

Sounds like you just need one check of any error occuring as per my first comment and those of Bill and Qlemo since.

You can do checks with %errorlevel% but not needed in this case by the sounds of it.

@ECHO OFF
SETLOCAL EnableDelayedExpansion

:LOOP
  REM do stuff
  if errorlevel 1 exit /b %errorlevel%
goto loop

or

@ECHO OFF
SETLOCAL EnableDelayedExpansion

:LOOP
  REM do stuff
  if errorlevel 1 exit %errorlevel%
goto loop
thanks sorry for the very late award