PTim

Does Exit really update %ErrorLevel%

21 posts in this topic

#1 ·  Posted (edited)

The documentation states:

Exit [return code]

Parameters

return code    [optional] Integer that sets the script's return code. This code can be used by Windows or the DOS variable %ERRORLEVEL%. The default is 0. Scripts normally set an errorlevel of 0 if the script executed properly; error levels 1 and above typically indicate that the script did not execute properly.

I have not been able to get this to work, even a simple example provided does not work:

Exittest.au3

; Terminate script if no command-line arguments
If $CmdLine[0] = 0 Then Exit (1)

When you run this and type echo %ErrorLevel% at cmd prompt you always get 0.

Is there anyway to successfully get a script to provide the correct exit value to %ErrorLevel%.

For some strange reason if you create another batch file for example ex-test.bat:

@Echo off

Exittest.exe

echo %ErrorLevel%

The errorlevel is returned successfully.

Can anyone help me out.

I need my AutoIt exe to be able to set the %ErrorLevel% as this is being picked up by another process.

Thanks in advance

 

 

Edited by PTim

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Sure it does:

$Return = RunWait(@AutoItExe & ' /AutoIt3ExecuteLine  "Exit 1"')
ConsoleWrite($Return & @CRLF)

$Return = RunWait(@AutoItExe & ' /AutoIt3ExecuteLine  "Exit 0"')
ConsoleWrite($Return & @CRLF)

If you take the help file example script, and compile it, and then run it in a command prompt, you will get the error levels.

Edited by jdelaney

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites

jdelaney sorry I am new to AutoIt and do not understand what your sample is trying to do.

The sample in the help file does not work when compiled:

; Terminate script if no command-line arguments
If $CmdLine[0] = 0 Then Exit (1)

Based on the info in the help I would expect this to compile and when run with no command line arguments, that at the command prompt when you do an "echo %ErrorLevel%" then %ErrorLevel% would return 1.  However it is always 0.

To help I have written a couple of test scripts:

mytest.bat:

@Echo off

set myerr=1
if not exist file.exe goto myexit

exit /b %myerr%

:myexit

set myerr=99


exit /b %myerr%

So all this does is look for file.exe in the directory you are running for, if it exists then it will exit with 1 (on checking error level at cmd prompt after running "echo %ErrorLevel%" will return 1.  If the file does not exist it will return 99.

When I run this from my Autoit script: (Exit_tst.au3)

#include <Constants.au3>
#include <MsgBoxConstants.au3> 
Local $Result
Local $command

$Command = "mytest.bat"
$Result = RunWait($Command)
ConsoleWrite($Result & @CRLF)
MsgBox($MB_SYSTEMMODAL, "Test", "Result: " & $Result)
Exit $Result

When compiled and run from command prompt, and I check ErrorLevel by: "echo %ErrorLevel%"  it always returns 0.

In the scenario where file.exe in the batch file does not exist $Result contains 99 (hence the MsgBox debug line).

What I expect is that by using Exit $Result, it would populate the ErrorLevel in the command window.

Can anyone explain what I am doing wrong?

 

 

mytest.bat

Share this post


Link to post
Share on other sites

http://blogs.msdn.com/b/oldnewthing/archive/2008/09/26/8965755.aspx

BTW, everything in your batch file can be done inside the autoit script, there's no need to use a batch file for this.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

I know - the test batch file was just to illustrate the problem.

In real life I need to call an existing process that will that will return specific codes.

I need to use an AutoIt exe to control the execution of this process and the parameters passed to this process.

Thanks for the blog.  I have read that before and read it again.

I still don't follow it apart from the fact that you cannot rely on errorlevel being what you want.

When the test batch file is run (outwith my AutoIt exe) - the return code from the script is written to %ErrorLevel% correctly.

I still don't understand why this return code held in "$Result"  and "Exit $Result" does not do the same as the "exit /b %myerr%" from the batch file. - i.e. populate ErrorLevel correctly. ;-(

The documentation states:

Exit [return code]

Parameters

return code    [optional] Integer that sets the script's return code. This code can be used by Windows or the DOS variable %ERRORLEVEL%. The default is 0. Scripts normally set an errorlevel of 0 if the script executed properly; error levels 1 and above typically indicate that the script did not execute properly.

So based on that...my interpretation is that it could be used with the DOS variable %ERRORLEVEL% - however it does not appear to do so.

Share this post


Link to post
Share on other sites

Any ideas on how I can get the return code of the process I am calling to be written to %ErrorLevel% or is it just not possible?

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Yes, just figured it out

#pragma compile(Console, true)

Exit (1)

Help file should probably say only sets ERRORLEVEL if compiles as console app.

EDIT: actually that does not even work. I'd forgot I done "set ERRORLEVEL=1" while testing.

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

AuotIt can retrieve the exitcode of the batch file, as it seems to be working as far as that goes. What are you using to retrieve the exit code of AutoIt? Because if it's another batch file that is launching the AutoIt exe, then you can use, in the batch file, "If Errorlevel" to do something with the exitcode of AutoIt.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

AdamUL, I hope you realize that you just posted the exact same link that I did 6 posts before yours.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

That's what I get for not reading the full thread.  

 

Adam

 

Share this post


Link to post
Share on other sites

After some further research, give this a try. Compile a script with just Exit 1 as test.exe.

Exit 1

Then run it in a CMD window or batch file.  Using start /wait starts the script, and then waits for it to return with the exit code to the %errorlevel% variable.  

start /wait test.exe

echo %errorlevel%

 

Adam

 

Share this post


Link to post
Share on other sites

AdamUL - that appears to work, but don't know if it will suit my needs, will do some more checking tomorrow.  Thanks

I can't put my finger on why that works or why calling autoit exe from a batch file works - with respect to populating %ErrorLevel%, and it doesn't by just executing the exe straight from command line.  Again will look into further tomorrow.

 

Thanks to all that have replied to this topic.  I really appreciate all of your inputs. :)

 

 

Share this post


Link to post
Share on other sites

#18 ·  Posted (edited)

The only way I can get it to work is...

#pragma compile(Console, True)

Run(@ComSpec & ' /k ' & 'set ERRORLEVEL=2')

Exit 2

EDIT: actually this is working now :blink:

#pragma compile(Console, True)

Exit 4

Things that changed... Upper case T and no parenths around exit code.

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

Thanks all.

I have done some quick tests and it would appear that there are 3 possible ways identified to get this working:

1) launch my AutoIt exe from a batch file - %ErrorLevel% appears to get set this way. (which I had already identified in my original post)

2) launch my AutoIt exe from command file with: start /wait test.exe - %ErrorLevel% appears to get set this way.  (supplied by AdamUL)

3) By Compiling as a  console app i.e. #pragma compile(Console, True)  - %ErrorLevel% appears to get set this way.  (Supplied by JohnOne)

Now I plan to do some proper testing and further research into figuring out why these work.

I was trying to find out what compiling as console implies, I am halfway through the help file.  If anyone has a simple summary of what compiling like this actually means to the execution of the exe, and any implications that doing this would have on the target systems where I will be deploying these script tools, then please let me know.  Thanks.

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now