0

I have a parent scipt:test.bat

@Echo off
SETLOCAL ENABLEDELAYEDEXPANSION
start cmd /c a.bat ^& echo %errorLevel% ^> exitcode.txt

if exist exitcode.txt (
    echo ERROR in a.bat file
)
ECHO Reached the EOF

The child scipt:a.bat

echo off
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /L %%A IN (1,1,1000) DO (
echo value in loop %%A 
)
exit /b 0

I want to check the errorlevel in test.bat only after a.bat is completed. NOTE: a.bat can be called from test.bat more than once. All a.bat will run parallelly. And Test.bat will wait till all a.bat file execution is completed. Now, the test.bat is getting completed before a.bat completes. Can anyone help on this?

aschipfl
  • 33,626
  • 12
  • 54
  • 99

1 Answers1

0

To use start, you would need to modify all the subprograms to create an exitcode file on error.

SEE HERE FOR HOW TO HOLD PARENT PROGRAM UNTIL ALL SUBS COMPLETED

@Echo off
SETLOCAL ENABLEDELAYEDEXPANSION

:controller

Call :launcher "a.bat" "b.bat" "c.bat"

pause
EXIT

:launcher
Set count=0
For %%a in (%*) Do (
Set /a count+=1
Set "Prog[!count!]=%%~a"
Start "+++batch+++" "%%~a"
)

:loop
    timeout /t 1 >nul
    tasklist /fi "windowtitle eq +++batch+++*" |find "cmd.exe" >nul && goto :loop

REM :: Note- This method exits subs on encountering First Error.

For /L %%B IN (1,1,!count!) DO (
FOR %%N IN (!Prog[%%B]!) DO (
    IF EXIST "%%~nN_exitcode.txt" (
        <"%%~nN_exitcode.txt" (
        Set /p exitcode=
        Set /p E_P=
        Set /p L_N=
        )
        DEL /Q "%%~nN_exitcode.txt"
        IF DEFINED exitcode (
        ECHO Errorlevel !exitcode! returned in !E_P! at Line !L_N!
        )
    ) else (
        ECHO Sub %%~nN completed succesfully
        )
    )
)

GOTO :EOF

Child Program Examples:

a.bat

@ECHO OFF
Setlocal EnableDelayedExpansion
CALL Hello|| (ECHO(!errorlevel!&ECHO(%~nx0&ECHO(3)>%~n0_exitcode.txt && EXIT
ECHO HELLO|| (ECHO(!errorlevel!&ECHO(%~nx0&ECHO(4)>%~n0_exitcode.txt && EXIT
exit

b.bat:

@ECHO OFF
Setlocal EnableDelayedExpansion
ECHO Hello|| (ECHO(!errorlevel!&ECHO(%~nx0&ECHO(3)>%~n0_exitcode.txt && EXIT
CALL HELLO|| (ECHO(!errorlevel!&ECHO(%~nx0&ECHO(4)>%~n0_exitcode.txt && EXIT
exit

c.bat

@ECHO OFF
Setlocal EnableDelayedExpansion
ECHO Hello|| (ECHO(!errorlevel!&ECHO(%~nx0&ECHO(3)>%~n0_exitcode.txt && EXIT
ECHO HELLO|| (ECHO(!errorlevel!&ECHO(%~nx0&ECHO(4)>%~n0_exitcode.txt && EXIT
exit

The || (ECHO(!errorlevel!&ECHO(%~nx0&ECHO(4)>%~n0_exitcode.txt && EXIT creates the file only in the event the preceeding command fails.

  • ECHO(!errorlevel! Stores the Errorlevel to the First line of Sub Specific Exitcode file.
  • ECHO(%~nx0 Stores the filename and extension to the second line.
  • ECHO(n Stores the Line Number to the third line. You'll need to adjust this Number to the Line in your Code the command Using this Debugging tool is Positioned at.
T3RR0R
  • 2,747
  • 3
  • 10
  • 25