batch-filewindows-xp

How can one batch file get the exit code of another?


I have two batch files, task.bat and runtask.bat. The runtask.batcalls task.batand I would like runtask.bat to get the exit code of task.bat into a variable. How could this be done?

task.bat:

@echo off
set errorlevel=1

runtask.bat

...
CMD /C task.bat
set taskexitcode=????

Solution

  • Just swap CMD /C for call.

    task.bat:

    @echo off
    set errorlevel=15
    

    runtask.bat

    call task.bat
    set taskexitcode=%errorlevel%
    echo %taskexitcode%
    

    Output

    15