Solved

Command Line Extensions - ErrorLevel?

Posted on 2011-09-13
16
785 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 54

Expert Comment

by:Bill Prew
ID: 36532353
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 54

Expert Comment

by:Bill Prew
ID: 36532360
A bit more info here as well.

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

~bp
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 36532821
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
Portable, direct connect server access

The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

 
LVL 43

Expert Comment

by:Steve Knight
ID: 36532979
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
ID: 36536252
@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
ID: 36536293
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 54

Expert Comment

by:Bill Prew
ID: 36536454
==> 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 54

Expert Comment

by:Bill Prew
ID: 36536466
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
 
LVL 54

Expert Comment

by:Bill Prew
ID: 36536485
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
ID: 36536647
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 54

Accepted Solution

by:
Bill Prew earned 125 total points
ID: 36536719
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 69

Expert Comment

by:Qlemo
ID: 36537216
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
ID: 36537495
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 54

Expert Comment

by:Bill Prew
ID: 36537515
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
ID: 36539736
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
ID: 36924784
thanks sorry for the very late award
0

Featured Post

Manage your data center from practically anywhere

The KN8164V features HD resolution of 1920 x 1200, FIPS 140-2 with level 1 security standards and virtual media transmissions at twice the speed. Built for reliability, the KN series provides local console and remote over IP access, ensuring 24/7 availability to all servers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Even if you have implemented a Mobile Device Management solution company wide, it is a good idea to make sure you are taking into account all of the major risks to your electronic protected health information (ePHI).
For many of us, the  holiday season kindles the natural urge to give back to our friends, family members and communities. While it's easy for friends to notice the impact of such deeds, understanding the contributions of businesses and enterprises i…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

735 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