How to get exit code from command prompt?

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
bdeshi
Posts: 4256
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612
Contact:

How to get exit code from command prompt?

Post by bdeshi »

How do I get the exit code of a command with runret?
runret returns the standard output only. So I thought of echoing %errorlevel% to stdout, but this doesn't work:

Code: Select all

::echo runret("%COMSPEC% /c reg QUERY HKCU\Environment /ve & echo %errorlevel%", "%WINDIR%\system32\");
::echo runret("%COMSPEC% /c reg QUERY HKCU\Environment /v NONEXISTENT_VALUE & echo %errorlevel%", "%WINDIR%\system32\");
Both return errorlevel as 0 even though the second one should fail.

I know about exit-code based conditional running with && and ||, but this still doesn't give me the actual exitcode.

Code: Select all

::echo runret("%COMSPEC% /c reg QUERY HKCU\Environment /ve && echo success || echo fail", "%WINDIR%\system32\");
::echo runret("%COMSPEC% /c reg QUERY HKCU\Environment /v NONEXISTENT_VALUE && echo success || echo fail", "%WINDIR%\system32\");
No help when the exit code is something other than 0 and 1.
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

highend
Posts: 14996
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: How to get exit code from command prompt?

Post by highend »

Maybe you get the exit code from calling %COMSPEC% in this case?
One of my scripts helped you out? Please donate via Paypal

bdeshi
Posts: 4256
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612
Contact:

Re: How to get exit code from command prompt?

Post by bdeshi »

without running via comspec, only the reg command's output is returned, no access to the echo commands. And reg says syntax invalid.

Code: Select all

::echo runret("reg QUERY HKCU\Environment /ve & echo %errorlevel%", "%WINDIR%\system32\");
::echo runret("reg QUERY HKCU\Environment /ve && echo 0 || echo 1", "%WINDIR%\system32\");
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

highend
Posts: 14996
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: How to get exit code from command prompt?

Post by highend »

Sure, it's not running inside %comspec% any more :) That seems like a similar cornercase we had the last time with runret.
One of my scripts helped you out? Please donate via Paypal

bdeshi
Posts: 4256
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612
Contact:

Re: How to get exit code from command prompt?

Post by bdeshi »

there's possibility of a wish for an additional parameter of runret to return the exitcode.
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

admin
Site Admin
Posts: 66678
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: How to get exit code from command prompt?

Post by admin »

Next beta pass 2 as flags. But it will always return 259 (STILL_ACTIVE) I think. Not much use...?

bdeshi
Posts: 4256
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612
Contact:

Re: How to get exit code from command prompt?

Post by bdeshi »

Thanks but that wouldn't help at all obviously. Ideally it should return the proper exit code after the process has exited.
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

admin
Site Admin
Posts: 66678
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: How to get exit code from command prompt?

Post by admin »

I can loop WaitForSingleObject until it's finished. Then get the exitcode. Ok?

bdeshi
Posts: 4256
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612
Contact:

Re: How to get exit code from command prompt?

Post by bdeshi »

Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

bdeshi
Posts: 4256
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612
Contact:

Re: How to get exit code from command prompt?

Post by bdeshi »

Molto grazie! :kidding:
Works perfectly fine with the reg examples (obviously except the multi-commands).
Tested with a simple ahk script too: Exit, <ExitCode> ; Returns the proper <exitcode>
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

admin
Site Admin
Posts: 66678
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: How to get exit code from command prompt?

Post by admin »

Cool! :beer:

BTW, your link never worked. Site is dead.

bdeshi
Posts: 4256
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612
Contact:

Re: How to get exit code from command prompt?

Post by bdeshi »

weird. The page comes up intact on my browser...

There's no point anymore, but this link might work (Internet Archive): http://wayback.archive.org/web/20160103 ... /Tip/18663
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

admin
Site Admin
Posts: 66678
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: How to get exit code from command prompt?

Post by admin »

OK, that worked. It's pretty the same what I did.

Post Reply