user2432405
user2432405

Reputation: 1414

How to continue the effect of "setlocal ENABLEDELAYEDEXPANSION" after "endlocal"?

Here are two batch files : 1.bat, setenv.bat
1.bat:
call setenv.bat
echo %var%
setenv.bat:
setlocal ENABLEDELAYEDEXPANSION
...
for ... do (
if ... (
set var=!other!
)
)
endlocal
delayed expansion is needed in setenv.bat for some reason. setenv.bat is used to set some environment variables, which used in other files, 1.bat here.
But even if var is set in setenv.bat correctly, 1.bat still uses the old value of var: Say var=old before calling setenv.bat, and setenv.bat change the value of var to new, but the output of 1.bat is still old, which I need it to be new.
Any way to gain the effect of delayed expansion without localizing the in-course modification?

Upvotes: 3

Views: 329

Answers (2)

Magoo
Magoo

Reputation: 79983

At the very end of setenv.bat:

...
endlocal&set "var=%var%"&set "var2=%var2%"

Upvotes: 3

Rafael
Rafael

Reputation: 3112

You can try FOR /F to keep your variable:

setenv.bat:

setlocal ENABLEDELAYEDEXPANSION
set other=new
...
for /f "delims=" %%V in (""!other!"") do (
 endlocal
 for ... do (
  if ... (
   set var=%%~V
  )
 )
)

Upvotes: 2

Related Questions