Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 797
  • Last Modified:

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.
0
sbornstein2
Asked:
sbornstein2
  • 7
  • 5
  • 2
  • +1
1 Solution
 
Bill PrewCommented:
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
0
 
Bill PrewCommented:
A bit more info here as well.

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

~bp
0
 
QlemoC++ DeveloperCommented:
Correct. ERRORLEVEL can only be used in IF, while %ERRORLEVEL% (or !ERRORLEVEL!, if Delayed Expansion is enabled) is available whenever other environment vars are.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Steve KnightIT ConsultancyCommented:
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
0
 
sbornstein2Author Commented:
@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
0
 
sbornstein2Author Commented:
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
0
 
Bill PrewCommented:
==> 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
0
 
Bill PrewCommented:
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
0
 
Bill PrewCommented:
Some discussion of the EXIT /B issue on earlier versions of the task scheduler here:

http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/Q_26968585.html

~bp
0
 
sbornstein2Author Commented:
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

0
 
Bill PrewCommented:
Yes, as long as ERRORLEVEL never has a negative value that's better.  The last line could likely just be a GOT at that point, since you know the value is 0 (or less) at that point.

~bp
0
 
QlemoC++ DeveloperCommented:
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.
0
 
sbornstein2Author Commented:
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.
0
 
Bill PrewCommented:
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
0
 
Steve KnightIT ConsultancyCommented:
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
0
 
sbornstein2Author Commented:
thanks sorry for the very late award
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 7
  • 5
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now