[英]Escape “double quotes” inside batch's input parameters
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
已经包含一个"
(因为它是一个路径,由上下文菜单传递给Send To传递 - 我对此没有多少控制权。)所以当命令在批处理脚本中运行时,它运行如下:
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
,使得它逃过了引号?
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: ,
&&
和&
例如:
echo ^|
but this is pretty rare. 但这种情况非常罕见。
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
更改为myPath
。 PATH is a reserved system variable that should not be used for anything other than what it was intended. PATH是一个保留的系统变量,不应该用于任何其他目的。
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, eg the escaped quotes. 只需从你的参数中删除原始引号(使用
%~1
) - 然后你可以再添加你想要的任何内容,例如转义的引号。 In your example: 在你的例子中:
runas /user:abc "icacls \"%~1\" /grant Everyone:(F) /T"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.