belltric belltric - 1 year ago 75 Question

Get Return Value from Console Application in Batch file

I'm trying to get return value from console application in batch file. Console application keep returns 0 value even when the rtnValue is 1 in console application

Console Application Code

Public Function Main() As Integer
Dim rtnValue as Int16 = 0

if Not Process Then rtnValue = 1

Return rtnValue

End Function

Batch File


start /d "%~dp0" ConsoleApp.exe

if "%ERRORLEVEL%" == "0" goto success

echo Success with error code: %ERRORLEVEL%
goto end

echo Failed with error code: %ERRORLEVEL%


Answer Source

Don't use start /d "%~dp0" ConsoleApp.exe that is launching a new shell to run it in. Instead just use

cd %~dp0

and then test for the exit code.