[英]Is it possible to echo a string including path with spaces within FOR/IF statement without adding quotes to the output?
If you execute this: 如果执行此操作:
if "true" == "true" (
echo Path is C:\Program Files (x86)\MyFolder
)
You will get an error: 您将得到一个错误:
\MyFolder was unexpected at this time.
This is the same if a path is passed to echo as a variable. 如果将路径作为变量传递给echo,这也是相同的。
There is a solution. 有一个解决方案。 This can be fixed by surrounding the text for echo with double quotes (or just the path itself). 可以通过用双引号将回声文本括起来(或仅路径本身)来解决此问题。
However this adds double quotes to the output text: 但是,这会在输出文本中添加双引号:
Path is "C:\Program Files (x86)\MyFolder"
Is there any way to handle this situation without altering output? 有什么办法可以在不更改输出的情况下处理这种情况?
As already stated in the comment by @compo , the closing parenthesis is causing the issue as the system thinks it is the closure of your earlier opening parenthesis. 如@compo的注释中所述, 右括号引起了该问题,因为系统认为这是您先前的右括号的关闭。 You therefore need to escape the parenthesis ^)
因此,您需要转义括号^)
However as stated by you, this does not help you when you have an unknown output result, I however do do really understand why the quoted path is problem in the output. 但是,正如您所说,当输出结果未知时,这对您没有帮助,但是我确实确实理解了为什么在输出中引用的路径是问题。 but anyway, therefore you need to work around the issue, here are ways, depending on how your actual script layout. 但无论如何,因此您需要解决此问题,以下是一些方法,具体取决于您的实际脚本布局。
First option, Do not use parenthesized if
code block: 第一种选择, if
代码块,请不要使用括号:
if "true" == "true" echo path is %ProgramFiles(x86)%\MyFolder
The above method however will not allow you to do else statements and you would need multple if
statements. 但是上述方法将不允许您执行else语句,并且您将需要多个if
语句。
The other, better option would be to simply set the variable if there is a match.. Depending on your code blocks, these might require delayedexpansion
另一个更好的选择是,如果存在匹配项,则简单地设置变量。根据您的代码块,这些可能需要delayedexpansion
if "true" == "true" (
set "mypath=%ProgramFiles(x86)%\MyPath"
) else (
set "mypath=%ProgramFiles(x86)%\AnotherPath"
)
echo %mypath%
To echo an arbitrary text including special characters you could assign the text to a variable and then apply delayed variable expansion when echoing: 要回显包含特殊字符的任意文本,可以将文本分配给变量,然后在回显时应用延迟的变量扩展 :
set "EchoPath=C:\Program Files (x86)\MyFolder"
setlocal EnableDelayedExpansion
if "true" == "true" (
echo(!EchoPath!
)
endlocal
The setlocal
command enables delayed expansion here, the exclamation marks around the variable actually use it. setlocal
命令在此处启用延迟扩展,变量周围的感叹号实际使用它。 The endlocal
command ends delayed expansion. endlocal
命令结束延迟的扩展。 Note that any changes to the variable EchoPath
since setlocal
become lost past endlocal
. 请注意,自setlocal
以来,对变量EchoPath
任何更改EchoPath
丢失到endlocal
。
The odd-looking syntax echo(
is used here to avoid trouble with certain strings like on
and off
(which are special keywords), or a string like /?
, all of which could theoretically be stored in the variable. Consult also the following threads about that: 看起来很奇怪的语法echo(
在这里用来避免某些字符串的麻烦,例如on
和off
(这是特殊的关键字),或者类似/?
的字符串,理论上所有这些都可以存储在变量中。也请参考以下线程关于那个:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.