Batchfile: Problems to get errorlevel, it is always 0

73 Views Asked by At

scripters, at first sorry for the last post.

Realy, i hope i could make you understand my problem now.

I realized that the problem is inside my script only.

If i run the line in the command prompt it works fine. That means the Errorlevel is stored to exitCode.txt correctly.

START /B "some title" cmd /C "subroutine.bat& >"exitCode.txt" (call echo %^errorlevel%)& >"status.txt" (echo done) & (if defined return call echo %^return%) >"return.txt"" 1>"stdOut.txt" 2>"stdErr.txt"

This is a part of my script So in :treath.run is the START /B .... line Inside the script the errorlevel is not stored correctly inside the definded file.

Why? The return Value is stored perfect.

I dont't get it.

Save this as main.bat

        @echo off
        setlocal ENABLEDELAYEDEXPANSION ENABLEEXTENSIONS
        pushd "%~dp0"


        :main
        call :treath.import
        call :treath.init "t1" "call subroutine.bat"
        call :treath.run "t1"
        :loop
        call :treath.status "t1"
        if "!return!"=="done" goto done
        echo !return!
        goto loop
        :done
        echo Now goto "!%t1%.stdExitCode!"
        echo.
        echo The Subroutine should return Errorlevel 1
        echo But it is 0, in the file.
        pause
        EXIT
        goto main
        :::::::::::::::::::::::::::::::::::::::



        :treath.import ()
        set "return="
        setlocal
        set "objID=__OBJ_TREATH"

        set "workdir=%tmp%\BATCH_OBJ\%objID%"
        if not exist "%workdir%" (mkdir "%workdir%" || exit /b 1)

        set /a "timeout=999"
        :newSessionID
            set /a "sessionID=%random%"
            set "sessionDir=%workdir%\%sessionID%"
            if exist "%sessionDir%" (
                set /a "timeout-=1"
                if !timeout! LSS 0 exit /b 1
                goto newSessionID
            )
        mkdir "%sessionDir%" || exit /b 1

        endlocal & (
            rem set "%objID%_identifier=%objID%"
            set "%objID%_version=0"
            set "%objID%_sessionID=%sessionID%"
            set "%objID%_sessionDir=%sessionDir%"
        )
        exit /b 0
            

        :treath.init (treathName,"command")
        set "return="
        setlocal
        rem hardcoded obj ident
        set "objID=__OBJ_TREATH"
        rem objID

        set "prefix=%objID%_%~1"
        rem treath folder
        set "workdir=!%objID%_sessionDir!\%~1"
        mkdir "%workdir%" || exit /b 1

        set "command=%~2"

        endlocal & (
            
            set "%prefix%.command=%command%"
            set "%prefix%.workdir=%workdir%"
            set "%prefix%.stdOut=%workdir%\stdOut.txt"
            set "%prefix%.stdErr=%workdir%\stdErr.txt"
            set "%prefix%.stdDump=%workdir%\stdDump.txt"
            set "%prefix%.stdExitCode=%workdir%\stdExitCode.txt"
            set "%prefix%.return=%workdir%\return.txt"
            set "%prefix%.stdStatus=%workdir%\stdStatus.txt"
            set "%~1=%prefix%"
        )
        exit /b 0

        :treath.run (treathName)
        set "return="
        setlocal
        set "title=%~1"
        set "prefix=!%~1!"


        type NUL >"!%prefix%.stdExitCode!"
        type NUL >"!%prefix%.stdOut!"
        type NUL >"!%prefix%.stdErr!"
        type NUL >"!%prefix%.stdStatus!"


        rem errorlevel ist immer 0 ???
        START /B "%title%" cmd /C "!%prefix%.command!& >"!%prefix%.stdExitCode!" (call echo %%^errorlevel%%)& >"!%prefix%.stdStatus!" (echo done) & (if defined return call echo %%^return%%) >"!%prefix%.return!"" 1>"!%prefix%.stdOut!" 2>"!%prefix%.stdErr!"

        >"!%prefix%.stdStatus!" (echo running)
        exit /b 0



        :treath.status (treathName)
        set "return="
        setlocal
        set "prefix=!%~1!"
        endlocal & (
            set /p "return="<"!%prefix%.stdStatus!" || exit /b 1
        )
        exit /b 0

Save this as "subroutine.bat"

set "return=This is a Test"
exit /b 1
0

There are 0 best solutions below