Page 1 of 1

How to get exit code from command prompt?

Posted: 09 Sep 2016 07:47
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.

Re: How to get exit code from command prompt?

Posted: 09 Sep 2016 09:09
by highend
Maybe you get the exit code from calling %COMSPEC% in this case?

Re: How to get exit code from command prompt?

Posted: 09 Sep 2016 09:19
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\");

Re: How to get exit code from command prompt?

Posted: 09 Sep 2016 09:30
by highend
Sure, it's not running inside %comspec% any more :) That seems like a similar cornercase we had the last time with runret.

Re: How to get exit code from command prompt?

Posted: 09 Sep 2016 09:45
by bdeshi
there's possibility of a wish for an additional parameter of runret to return the exitcode.

Re: How to get exit code from command prompt?

Posted: 09 Sep 2016 11:35
by admin
Next beta pass 2 as flags. But it will always return 259 (STILL_ACTIVE) I think. Not much use...?

Re: How to get exit code from command prompt?

Posted: 09 Sep 2016 13:00
by bdeshi
Thanks but that wouldn't help at all obviously. Ideally it should return the proper exit code after the process has exited.

Re: How to get exit code from command prompt?

Posted: 09 Sep 2016 13:13
by admin
I can loop WaitForSingleObject until it's finished. Then get the exitcode. Ok?

Re: How to get exit code from command prompt?

Posted: 09 Sep 2016 13:18
by bdeshi

Re: How to get exit code from command prompt?

Posted: 09 Sep 2016 18:20
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>

Re: How to get exit code from command prompt?

Posted: 09 Sep 2016 18:34
by admin
Cool! :beer:

BTW, your link never worked. Site is dead.

Re: How to get exit code from command prompt?

Posted: 09 Sep 2016 18:41
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

Re: How to get exit code from command prompt?

Posted: 09 Sep 2016 18:50
by admin
OK, that worked. It's pretty the same what I did.