Search code examples
batch-filereturn-value

Store return value of a compiled exe into a batch variable


I've read topics here and on the internet about the argument but the solutions offered do not work for me. Firstly I'll tell you what I want to do: I have a compiled c file (.exe) that returns various integers depending on the situation. I want to store said return value in a variable in batch. From what I've read, there's no specific command to do this (like 'v=$?` in shell that assigns to the variable the last returned value), but I found instead a workaround that uses the for loop.
The code I found is the following:

FOR /F "tokens=*" %%a in ('test.exe') do SET OUTPUT=%%a
echo %OUTPUT%

But when I run the batch file, I get ECHO is off.
I'm a complete beginner in batch, I simply searched "store return value in batch" and the code above is what got spit out. Any insight or help on the problem is appreciated, thanks in advance!


Solution

  • Answer given by Mofi

    Dillon Wreek wants the exit code (also called exit value, return value, return code or in CMD syntax errorlevel) of test.exe and not the output of the text.exe written to handle stdout. So all needed by Dillon Wreek is a batch file with first line @echo off, second line test.txt, third line echo %errorlevel% resulting in writing into console window the value 1 or 2 or 3 or ... 12 and fourth line pause to see the output on double clicking on batch file.

    Basically:

    test.exe
    set var=%errorlevel%
    echo %var%