views:

1010

answers:

5

Using the Windows XP CMD command-line I can expand a variable twice as follows:

set AAA=BBB
set BBB=CCC
for /F "usebackq tokens=*" %i in (`echo %%AAA%%`) do echo %i

will echo CCC. I.e. AAA has been expanded to the string BBB, and then the variable BBB has been expanded to CCC.

This doesn't work from inside a batch script (i.e. a .cmd file). Changing the %%AAA%% to either %%%AAA%%% or %%%%AAA%%%% doesn't work either.

Any idea how i can achieve this from within a script, namely to take expand the variable AAA to the string CCC?

Late Edit

The answers posted work for my reduced example however the non-tortuous answer doesn't work for the real case. Here's an extended example (which doesn't work), which illustrates what I was actually trying to do:

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!

)

I would like to see

111
222
333

output.

+2  A: 

The following (torturous) approach seems to work okay:

    @echo off
:main
    setlocal enableextensions enabledelayedexpansion
    set aaa=bbb
    set bbb=ccc
    call :myset x %%aaa%%
    echo %x%
    endlocal
    goto :eof
:myset
    for /F "usebackq tokens=*" %%i in (`echo %%%2%%`) do set %1=%%i
    goto :eof

It outputs:

ccc

as desired.

I've often used that trick to (for example) format %aaa% into %x% to a certain size (a la sprintf) but this is the first time I've had to do double indirection. It works because you don't find the extra "%%" being sucked up by the current shell level.

paxdiablo
A: 

Double indirection (which is a very apt way pax has put it) reminds of C pointer de-referencing.
I doubt that is supported in the Windows command shell.

Do you think the larger purpose you are targeting can be achieved with something easier;
maybe lesser of a code-golf approach then the one contrived nicely by pax?

nik
Probably, i think that would require a separate question though otherwise the current answers won't make sense.
jon hanson
+2  A: 

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!)
akf
Although this is indeed less tortuous, the var i'm actually using is part of a for loop, e.g. instead of AAA i have i :for %%i in (%LIST%) do (:: use iFor this case substituting i for AAA in your solution doesn't seem to work. Any ideas?
jon hanson
I'm not sure that I follow what you are saying. When using the var of a `for` loop in a bat, the double % should do the trick. I used %%a, which would be the same as %%i in your example. Perhaps you could include your more specific example in the question.
akf
I've added another example to the question.
jon hanson
Thanks. Please see my edits.
akf
Thanks, both of those do indeed work. I have to admit, after years of writing fairly advanced UNIX bash scripts, writing Windows cmd scripts has been a painful exercise. The myriad ways of expanding variables (%A, %%A, %A%, !%A%!, etc) is baffling. The enabledelayedexpansion flag appears to improve the situation but it inexplicably renders certain parts of my scripts inoperable. I guess PowerShell is the way forward...
jon hanson
Yeah, it certainly is not intuitive. The downvote for not using PowerShell was harsh, though.
akf
+1  A: 

This aaa.bat

@echo off
set aaa=bbb
set bbb=ccc
for /F %%i in ('echo %%%aaa%%%') do echo %%i

outputs

c:>ccc

What exactly is the trouble?

wqw
If you try it with upper-case letters for variable names as per my example it doesn't work. You get %CCC%.
jon hanson
A: 

use cygwin instead. it does what you want. ignore the fact you have to install cygwin to do this. it's worth it.

from: http://wiki.apache.org/nutch/GettingNutchRunningWithWindows

Cygwin

You'll need cygwin to run the shell commands since there are no separate scripts for NT cmd (the NT cmd shell does not nest environments recursively). Mks ksh does not work correctly with the scripts. Make sure you have installed the utility 'uname' in cygwin.

joshua paul