windows 在批处理的输入参数中转义“双引号”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11893309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Escape "double quotes" inside batch's input parameters
提问by laggingreflex
I want to run the following in batch script where it takes an argument (a path)
我想在批处理脚本中运行以下内容,它需要一个参数(路径)
runas /user:abc "icacls %1 /grant Everyone:(F) /T"
but the argument %1
already contains a "
(because it's a path, passed on by context menu's Send To - I don't have much control over this).
So when the command runs in batch script it runs like this:
但是参数%1
已经包含一个"
(因为它是一个路径,通过上下文菜单的发送到传递 - 我对此没有太多控制权)。因此,当命令在批处理脚本中运行时,它的运行方式如下:
runas /user:abc "icacls "c:\folder" /grant Everyone:(F) /T"
So obviously I need to escape the "
s created by %1
. How do I perform string manipulation over %1
such that it escaped the quotes?
所以显然我需要转义"
由%1
. 如何执行字符串操作%1
以使其转义引号?
回答by nick
回答by Andrew Dennison
Each response covers part of the answer. Combining them using backslash & quote: \"
you get:
每个回答都涵盖了答案的一部分。使用反斜杠和引用将它们组合起来:\"
你得到:
runas /user:abc "icacls \"%~1\" /grant Everyone:(F) /T"
or you can doubled (double) quotes to escape them:
或者你可以加倍(双)引号来逃避它们:
runas /user:abc "icacls ""%~1"" /grant Everyone:(F) /T"
As a side note ^
is occasionally useful for escaping special characters such as <
, |
, >
, ||
, &&
, and &
For example:
作为旁注^
,有时可用于转义特殊字符,例如<
, |
, >
, ||
, &&
, and&
例如:
echo ^|
but this is pretty rare.
但这很少见。
回答by laggingreflex
SET myPath=%1
SET myPath=%myPath:"=\"%
runas /user:abc "icacls %myPath% /grant Everyone:(F) /T"
Edit - The variable name was changed from path
to myPath
. PATH is a reserved system variable that should not be used for anything other than what it was intended.
编辑 - 变量名称已从 更改path
为myPath
。PATH 是一个保留的系统变量,不应用于除预期用途之外的任何其他用途。
回答by Drarakel
A search/replace isn't even needed in such cases. Just strip the original quotes from your argument (with %~1
) - then you can add again whatever you want, e.g. the escaped quotes. In your example:
在这种情况下甚至不需要搜索/替换。只需从您的参数中删除原始引号(使用%~1
) - 然后您可以再次添加任何您想要的内容,例如转义引号。在你的例子中:
runas /user:abc "icacls \"%~1\" /grant Everyone:(F) /T"