简体   繁体   English

批处理 - 获取包含特殊字符的参数

[英]Batch - get parameter that include special characters

I know there are many similar question but still not able to make it work.我知道有很多类似的问题,但仍然无法使其工作。

I have a simple batch that accepts several parameters, one of them is a password which may include special characters.我有一个接受多个参数的简单批处理,其中一个是可能包含特殊字符的密码。
The batch should run some .exe with the parameters that were sent to the batch.批处理应该运行一些带有发送到批处理的参数的 .exe。 Test.bat param1 param2 !@#$%^&*()_+ test.bat param1 param2 !@#$%^&*()_+

How should I handle the parameter with special characters ?我应该如何处理带有特殊字符的参数?
I tried using this code and also escaping the special characters with ^^ ^& etc.我尝试使用此代码并使用 ^^ ^& 等转义特殊字符。

  SET PASSWORD=%3

  SET "PASSWORD=%3"

  setlocal enabledelayedexpansion
  SET "PASSWORD=%3"
  echo !PASSWORD!

Important points:要点:

  • Doublequote the parameter Arg1 Arg2 "!@#$%~"^&*()_+"双引号参数Arg1 Arg2 "!@#$%~"^&*()_+"
  • Define the variable BEFORE enabling Delayed Expansion to preserve !在启用延迟扩展之前定义变量以保留! characters and any character following / between !字符和后面 / 之间的任何字符! characters.人物。
  • Doublequote the string from the start of the variable name to the end of the variable content, and Use the ~ modifier to remove the surrounding doublequotes during assignment of the expanded argument variable.将字符串从变量名的开头到变量内容的结尾用双引号括起来,并在扩展参数变量的赋值过程中使用~修饰符去除周围的双引号。
  • Use a caret escape during definition IE: Set^ "Arg3=%~3" to protect against unbalanced doublequotes.在定义 IE 期间使用脱字符转义: Set^ "Arg3=%~3"以防止出现不平衡的双引号。
@Echo off

 Set^ "Arg3=%~3"
 IF Defined Arg3 (
  Setlocal EnableDelayedExpansion
  Echo(!Arg3!
 )

 Endlocal

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM