批量字符串连接
我正在尝试创建一个像这样的批处理字符串: >abcd_
我有一个名为 soeid 的变量,其值为 abcd。所以这就是我正在做的事情,但它不起作用。
set soeid=abcd
set "val1=>"
set "val2=_"
set "str=%val1%%soeid%%val2%"
echo %str%
I am trying to create a batch string like this: >abcd_
I have a variable called soeid, with value as abcd. So this is what i am doing, but it does not work.
set soeid=abcd
set "val1=>"
set "val2=_"
set "str=%val1%%soeid%%val2%"
echo %str%
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我确信它工作得很好。为了证明这一点,请在定义值后添加
SET STR
,您将看到正确的值。您遇到的问题是,当您尝试回显该值时,正在执行的行变为:
echo >abcd_
。>
没有引用或转义,因此它只是获取不带参数的 ECHO 输出并将其重定向到名为“abcd_”的文件。如果您不介意看到引号,请更改您的行
echo "%str%"
就可以了。另一个选项是启用和使用延迟扩展(我假设这是批处理脚本代码,而不是在命令行上执行)
正常
%var%
扩展在解释器解析时早期发生线。延迟!var!
扩展发生在执行之前的末尾。在中间某处检测到重定向。这就是正常扩展不起作用的原因 - 解释器看到扩展的>
字符并将其解释为输出重定向运算符。延迟扩展会向解释器隐藏>
字符,直到解析重定向为止。有关延迟扩展的更多信息,请从命令行输入
SET /?
并从以“最后,支持延迟环境变量扩展...”开头的段落开始阅读。I'm sure it is working just fine. To prove it, add
SET STR
after you define the value, and you will see the correct value.The problem you are having is when you try to echo the value, the line that is executing becomes:
echo >abcd_
. The>
is not quoted or escaped, so it is simply taking the ouput of ECHO with no arguments and redirecting it to a file named "abcd_"If you don't mind seeing quotes, then change your line to
echo "%str%"
and it will work.The other option is to enable and use delayed expansion (I'm assuming this is a batch script code, and not executing on the command line)
Normal
%var%
expansion occurs early on while the interpreter is parsing the line. Delayed!var!
expansion occurs at the end just before it is executed. The redirection is detected somewhere in the middle. That is why the normal expansion doesn't work - the interpreter sees the expanded>
character and interprets it as the output redirection operator. The delayed expansion hides the>
character from the interpreter until after redirection is parsed.For more info about delayed expansion, type
SET /?
from the command line and read starting with the paragraph that starts with "Finally, support for delayed environment variable expansion...".