Decision
jeb works great. But this may not be acceptable under any circumstances. It has 2 potential disadvantages:
1) A syntax error will stop all batch processing. Therefore, if the script package is called your script, and your script is stopped with a syntax error, control is not returned to the caller. It could be bad.
2) Normally there is an implicit ENDLOCAL for each SETLOCAL when batch processing completes. But a fatal syntax error completes batch processing without implicit ENDLOCAL ! This can have unpleasant consequences: - (See My DosTips SETLOCAL post continues after batch completion! For more information.
Update 2015-03-20 See https://stackoverflow.com/a/166778/ for more info on how to immediately complete all batch processing.
Another way to stop the batch file inside the function is to use the EXIT command, which will exit the shell completely. But a little creative use of CMD can make it useful to solve the problem.
@echo off if "%~1" equ "_GO_" goto :main cmd /c ^""%~f0" _GO_ %*^" exit /b :main call :label hello call :label stop echo Never returns exit /b :label echo %1 if "%1"=="stop" exit exit /b
I have both my version with the name "daveExit.bat" and the jeb version with the name "jebExit.bat" on my PC.
Then I test them using this script package
@echo off echo before calling %1 call %1 echo returned from %1
And here are the results
>test jebExit before calling jebExit hello stop >test daveExit before calling daveExit hello stop returned from daveExit >
One of the potential drawbacks of the EXIT solution is that changes to the environment are not saved. This can be partially solved by writing the medium to a temporary file before exiting, and then reading it again.
@echo off if "%~1" equ "_GO_" goto :main cmd /c ^""%~f0" _GO_ %*^" for /f "eol== delims=" %%A in (env.tmp) do set %%A del env.tmp exit /b :main call :label hello set junk=saved call :label stop echo Never returns exit /b :label echo %1 if "%1"=="stop" goto :saveEnvAndExit exit /b :saveEnvAndExit set >env.tmp exit
But variables with a newline character (0x0A) in the value will not be stored properly.
dbenham
source share