问题描述
我有一个小问题,我工作的一个批处理文件。
I have a small problem with a batch file I'm working on.
下面是一个简单的例子:
Here's a simple sample:
我想获得字符串有为我的结果。
但结果我得到的仅仅是HELLO
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%%
和可悲的是,它只是让我的结果HELLO%%
任何帮助将是巨大的。谢谢!
And Sadly, it just gets me the result %HELLO%Any help would be great. Thanks!
推荐答案
之所以 %%结果%%
为您提供%结果%
在输出的是, %%
标记是相互preTED的首先,的进一步间pretation不这样做。
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:
@echo off
set result=hello
echo %result%
call :iset second %%result%%
echo %second%
goto :eof
:iset
for /F "usebackq tokens=*" %%i in (`echo %%%2%%`) do set %1=%%i
goto :eof
秘密在于传递东西你要跨preTED(在这种情况下, %%结果%%
通过在文字 %结果%
按照第一款规定的规则,的不的它的内部pretation)。
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).
的为
循环再回波的(你好
)包围的相互pretation %...%
(同样,双 %%
减少了%
),这样的是的也是跨preTED,它使用了设置你也通过了目标变量。
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:
%(%result%)%
这就是你追求的。
which is what you're after.
不过也许我建议,你开始寻找到PowerShell的,这样你就不必执行的将来,这些批次体操: - )
Might I suggest, however, that you start looking into Powershell, so that you don't have to perform these batch-gymnastics in future :-)
这篇关于批处理 - 如何回声%%变量%%的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!