Solved

Command Line Extensions - ErrorLevel?

Posted on 2011-09-13
16
782 Views
Last Modified: 2012-05-12
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
Comment
Question by:sbornstein2
  • 7
  • 5
  • 2
  • +1
16 Comments
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
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
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
A bit more info here as well.

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

~bp
0
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
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
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
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
 

Author Comment

by:sbornstein2
Comment Utility
@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
 

Author Comment

by:sbornstein2
Comment Utility
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
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
==> 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
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
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
 

Author Comment

by:sbornstein2
Comment Utility
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
 
LVL 51

Accepted Solution

by:
Bill Prew earned 125 total points
Comment Utility
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
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
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
 

Author Comment

by:sbornstein2
Comment Utility
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
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
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
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
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
 

Author Closing Comment

by:sbornstein2
Comment Utility
thanks sorry for the very late award
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Some time ago I was asked to set up a web portal PC to put at our entrance. When customers arrive, they could see a webpage 'promoting' our company. So I tried to set up a windows 7 PC as a kiosk PC.......... I will spare you all the annoyances I…
Don’t let your business fall victim to the coming apocalypse – use our Survival Guide for the Fax Apocalypse to identify the risks and signs of zombie fax activities at your business.
After creating this article (http://www.experts-exchange.com/articles/23699/Setup-Mikrotik-routers-with-OSPF.html), I decided to make a video (no audio) to show you how to configure the routers and run some trace routes and pings between the 7 sites…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now