Difficulty using set/endlocal in Batch across files -


i'm trying call 1 bat file another, while keeping target variables local itself(mostly). code below failed attempt, var want missing, local still around. i've found info on using setlocal here, how think have it. info i'm using push var past setlocal here. it's possible i'm missing these. appreciated!

the calling bat:

@set errmsg=old message @call target.bat test_job errmsg @echo.jobname = %jobname%, should undefined @if defined errmsg echo.error occurred: %errorlevel% %errmsg% 

the target bat:

@setlocal @echo off  :start set jobname=%~1 set errmsgvar=%~2  :main call:error "new error message" echo.should have never returned here endlocal  :exit @echo on @exit /b %errorlevel%  :error "errormesage" endlocal & set "%errmsgvar%=%~1" echo.%errmsg% ^<-- testing only, don't know actual var (goto) 2>nul & goto:exit 

the result:

c:\projects\batch>caller.bat new error message <-- testing only, don't know actual var jobname = test_job, should undefined error occurred: 0 old message 

edit clarity:

i'm trying accomplish several things here...

  • allow caller specify var error message should stored in | 2nd param in call
  • on error exit immediately, saving error message provided var | :error block
  • keep vars in target local, push error message through setting on endlocal line

it seems endlocal being ignored, , doing endlocal @ end of file, breaks things

as magoo said, test_job value cleared properly. test must have been looking @ holdover result prior run. best explicitly clear value in calling bat, prior call, can never false result..

your errmsg not being set because of flaw in logic. more on after provide little introduction (goto) 2>nul.

you attempting use relatively new (goto) 2>nul technique not known. exit /b except additional concatenated commands still execute, in context of caller. believe first discovery published @ http://forum.script-coding.com/viewtopic.php?id=9050 (which cannot read), , first known english posting @ http://www.dostips.com/forum/viewtopic.php?f=3&t=6491.

since dostips post, many useful tools have resulted:

within code, try endlocal in error routine, affects setlocal issued while in routine. need use (goto) 2>nul trick prior issuing endlocal endlocal works on parent context.

:error "errormesage" (   (goto) 2>nul   endlocal   set "%errmsgvar%=%~1"   goto :exit ) 

i'm worried returned errorlevel. don't think returning value want when there error.

you must remember external commands set errorlevel whether there error or not. internal commands set errorlevel non-zero upon error, , internal commands set errorlevel 0 upon success (but preserve existing errorlevel if there no error).

it safest explicitly pass desired error number :error routine, , explicitly set value upon exit. here 1 way it:

:error "errmsg" errnum (   (goto) 2>nul   endlocal   set "%errmsgvar%=%~1"   cmd /c exit %2   goto :exit ) 

but modify things exit same way regardless whether there error or not. :exit routine below called error message , error number if there error, , values set appropriately. upon normal exit, neither value passed in, errmsg variable gets cleared, , routine returns current errorlevel (presumably 0).

calling script

@setlocal @set errmsg=old message @set "jobname=" @call target.bat test_job errmsg @if defined jobname (   echo error - jobname = %jobname% ) else (   echo success - jobname undefined ) @if defined errmsg (   echo.error occurred: %errorlevel% %errmsg% ) else (   echo no error occurred ) 

target.bat

@echo off setlocal enabledelayedexpansion  :start set jobname=%~1 set errmsgvar=%~2  :main set /a "1/(%random% %% 3)" || call :exit !errorlevel! "random error" echo success call :exit   :exit  [errnum]  ["errmsg"] (   (goto) 2>nul   endlocal   set "%errmsgvar%=%~2"   echo on   exit /b %1 ) 

the :main code randomly raises error 33% of time when attempts divide zero.

here sample output, showing both possible outcomes:

c:\test>test success success - jobname undefined no error occurred  c:\test>test divide 0 error. success - jobname undefined error occurred: 1073750993 random error 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -