3
setlocal
for /F "tokens=1,2" %%a in (%env_cells%) do (
    call :env_setter env_setter_%%a
)
goto:EOF

:env_setter
rem doesn't do anything
endlocal
call %%1
goto:eof

The problem is the above disables the setting of global variables in all calls spawned by :env_call. Is there a way to resolve this?

Perhaps this is a more detailed explanation:

env_setter is a call to a batch file that itself contains set calls with the intention to set environment variables on this calling context. Because setlocal is enabled during the call to !env_cell! and the endlocal in :env_cell does not appear to disable endlocal, said set calls do not change this context as intended.

In this particular example, one can use %%a but I wish to understand this functionality in general.

Is there a manual way to access a variable value reflectively in a if/for block?

2
  • Do you want to 'move' variables from a 'delayed expansion' environment back to the 'start' environment?
    – Endoro
    Nov 16, 2013 at 14:33
  • Yup, I would like to be able to have set calls in the child process to effect the calling environment.
    – MetaChrome
    Nov 16, 2013 at 14:37

3 Answers 3

6

Throw your variables over the wall!

@echo off &setlocal
echo(

echo this obviously doesn't work
set "Counter="
for /l %%a in (1 1 5) do (
    SETLOCAL ENABLEDELAYEDEXPANSION
    set "Counter=!Counter!1"
    set "Variable=!Variable!!Counter!"
    echo !Counter!:!Variable!
    endlocal
)
echo Counter's value in the calling environment : %Counter%
echo Variable's value in the calling environment: %Variable%
echo(
    
echo throw your variables over the wall!
set "Counter="
for /l %%a in (1 1 5) do (
    SETLOCAL ENABLEDELAYEDEXPANSION
    set "Counter=!Counter!1"
    set "Variable=!Variable!!Counter!"
    echo !Counter!:!Variable!
    for /f %%a in ('set Counter^&set Variable') do (if "!"=="" endlocal)&set "%%a"
)
echo Counter's value in the calling environment : %Counter%
echo Variable's value in the calling environment: %Variable%
            
1
  • @MetaChrome I changed from 'numeric' to 'string' variables. The result is more clearly.
    – Endoro
    Nov 16, 2013 at 15:32
3

It's not possible to use an endlocal in a function to leave a setlocal which is set outside the function.

But when a syntax error occours all setlocal instances are still alive and can't be removed with endlocal, so this can set global variables.

setlocal
for /F "tokens=1,2" %%a in (%env_cells%) do (
    call :env_setter env_setter_%%a
)
goto:EOF

:env_setter
set myNewGlobal=1
call :haltAndStore
goto:eof

:haltAndStore
()
3
  • Is there a way to facilitate the functionality? The updated specified is interesting, but it is not relevant because it terminates the script.
    – MetaChrome
    Nov 16, 2013 at 14:15
  • I don't know a way to survive the halt (I believe there isn't any possiblity). But the way of foxidrive could be a solution
    – jeb
    Nov 16, 2013 at 14:31
  • I was refering to the possibility of accessing variables in the block in some other way reflectively. I have to go right now but I'm thinking you can write to disk or memory manually if that is possible.
    – MetaChrome
    Nov 16, 2013 at 14:39
0

I used @jeb answer to implement assigning IP addresses to environment variables. Possibly my worst hack ever.

@echo off
setlocal enabledelayedexpansion
set _count=1
for /f "usebackq tokens=*" %%a in (`ipconfig ^| findstr /i "ipv4"`) do (
    for /f delims^=^:^ tokens^=2 %%b in ('echo %%a') do (
        for /f "tokens=*" %%c IN ('echo %%b') do (set IP!_count!=%%c & echo IP!_count!=%%c & set /a _count+= 1)
    )
)
set _count=
call :haltAndStore 2> nul

:haltAndStore
()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.