I have a small problem with a batch file I'm working on.
Here's a simple sample:
I would like to get the string "THERE" as my result. But the result I get is just "HELLO"
set hello=there
set a=h
set b=ello
set result=%a%%b%
echo %result%
I already tried something like this:
Echo %%result%%
And Sadly, it just gets me the result %HELLO% Any help would be great. Thanks!
The reason that
%%result%%
gives you%result%
on the output is that the%%
tokens are interpreted first, and further interpretation is not done.However, you can use that to your advantage, doing a second level of indirection with the following trick:
The secret lies in passing the thing you want interpreted (in this case,
%%result%%
passes in the literal%result%
as per the rules stated in the first paragraph, not the interpretation of it).The
for
loop then echos the interpretation of that (hello
) surrounded by%...%
(again, the double%%
reduces to%
), so that it is also interpreted, and it uses that to set the target variable you also passed in.The upshot is that it effectively gives you:
which is what you're after.
Might I suggest, however, that you start looking into Powershell, so that you don't have to perform these batch-gymnastics in future :-)