1

I am currently running an EXE application which is called from a bat file with some other exes. What iam trying to do is i want to return a value from EXE and pass it to an other EXE in bat file. I modified Main in my console to return type as an integer but unable to access value in Bat file except in ERRORLEVEL which i dont want to do as it may erase any potential errors. If i use ERRORLEVEL to capture return value for instance an error occured and it returns a error integer, how can i distinguish between the integer value which iam returning and this value. Please let me know your input on this.

@echo off
set PATH=%PATH%;C:\Scripts
Bat File:
TestConsoleApplication.exe
ECHO %ERRORLEVEL% -- Want to capture here return value of EXE dont want to do it using ERRORLEVEL

2 Answers 2

2

If your EXE does not currently use stdout then you can simply write the results to stdout where it can be captured by the parent batch process.

For example, if your EXE (myprog.exe) were to output

var1=my first value
var2=23

then this simple script could be used to execute the EXE and capture and store the results

for /f "delims=" %%a in ('myprog.exe') do set "%%a"

Even if stdout is being used, you might be able to add the results to the output in such a way that you can parse out the values you need within the batch. But there is a simpler method if stdout is not convenient.

Have your EXE create a temporary batch file that sets the values. Your batch file can then call the temporary batch file after EXE completes and then delete the temp batch file.

Sign up to request clarification or add additional context in comments.

4 Comments

I am not using StdOut but i dint get you on how should i do this. I copied above script into my batch file but it does nothing. How should i use stdout in exe and is the above script complete for me to get value in my batch.
@user1098028 - Based on the way you wrote your question, I assumed you wrote the EXE. So to use the first solution you need to modify your EXE to print the values to stdout (to the screen). If you print the values as name=value, one per line, then you can use the FOR statement I gave to capture the results in batch environment variables. With my trivial example the variables would be named var1 and var2.
Thanks for the reply but i am still having problems. I modified my program to Dim myprocess As Process = New Process myprocess.StartInfo.FileName = "ping" myprocess.StartInfo.Arguments = i myprocess.StartInfo.UseShellExecute = False myprocess.StartInfo.CreateNoWindow = True myprocess.StartInfo.RedirectStandardInput = True myprocess.StartInfo.RedirectStandardOutput = True myprocess.StartInfo.RedirectStandardError = True Continued...
and batch file to @echo off set PATH=%PATH%;C:\Scripts var2=23 for /f "delims=" %%a in ('TestConsoleApplication.exe') do set "%%a" ECHO %var2% ping -n 5 127.0.0.1 >NUL. But it says var 2 unrecognised not sure what iam doing wrong.
1

Errorlevel will have the return value of the program you ran. You can't specify to not have it hold the return value of the program.

If you are worrying about other errors elsewhere in your script, you can check the errorlevel at those places and store the value in another variable or jump to a particular label that handles that error.

2 Comments

I am checking at each level of program but what my concern if an error occured in my current exe which i called. Its going to return a error value which iam not supposed to pass how can i handle this scenario.
errorlevel will have the return code of your program. If the error that occurs in your exe is not used as the return code, there will be no way for you to get the error.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.