Thinking in terms of a less tortuous solution, this, too, produces the CCC
you desire.
setlocal enabledelayedexpansion
set AAA=BBB
set BBB=CCC
for /F %%a in ('echo %AAA%') do echo !%%a!
edit:
to dissect this answer:
setlocal enabledelayedexpansion
- this will allow for any environment variable setting during your bat to be used as modified during the process of your for
loop.
set AAA=BBB
, set BBB=CCC
- your data population set
statements
for /F %%a in ('echo %AAA%') do echo !%%a!
- This tells the processor to loop, albeit only once, and take out the first token that is returned (default delimiter of space and tab apply) from the running of the command in the parens and put it in the var %%a (outside of a batch, a single % will do). If you specify that var as %%a, you need to use %%a in your do
block. Likewise, if you specify %%i, use %%i in your do
block. Note that to get your environment variable to be resolved within the do
block of the for
loop, you need surround it in !
's. (you don't need to in the in
block, as I originally posted - I have made that change in my edit).
edit:
You were very close with your updated example. Try it like this:
@echo off
setlocal enabledelayedexpansion
set LIST=BBB CCC DDD
set BBB=111
set CCC=222
set DDD=333
for %%i in (%LIST%) do (
for /F %%a in ('echo %%i') do echo !%%a!
)
The difference between your update and this is that you were trying to echo the environment variable in the in
set with in ('echo %%%i%')
, but without the !
's for the delayed expansion of set variables. Were you to use in ('echo !%%i!')
, you would see your BBB
, CCC
, and DDD
variables resolved, but then the do
block of your inner loop wouldnt have anything to resolve - you dont have any 111
environment variables. With that in mind, you could simplify your loop with the following:
@echo off
setlocal enabledelayedexpansion
set LIST=BBB CCC DDD
set BBB=111
set CCC=222
set DDD=333
for %%i in (%LIST%) do (echo !%%i!)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…