批处理文件中的特殊字符 [英] Special Characters in Batch File

查看:164
本文介绍了批处理文件中的特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

批处理文件中的特殊字符是一种痛苦,但是我没有找到正确的解决方法,正确地转义了我试图传递该应用程序的特定字符串的前两个字符。

  SET pass = ^& AntiBatchfileString 
A_Program.exe / pass =%pass%

我尝试过的东西:

  ::转义两次,第一个为^,第二个为& 
SET pass = ^^^^& AntiBatchfileString
echo%pass%

::组合转义。
SET first = ^^
SET second = ^^& AntiBatchfileString
SET pass =%first %% second%
echo%pass%

::防止扩展
SET first = ^^
SET second = ^^& AntiBatchfileString
SET pass =!first!%second%
echo%pass%

::我得到这个打印正确
SETpass = ^& AntiBatchfileString
echo ^^%pass%

当传递最后一个不接受登录时,我不知道最终的输出是什么。这让我想到也许这是尝试在将参数传递给应用程序时进行另一个扩展,所以我也引用了。

  SETpass = ^& AntiBatchfileString
A_Program.exe/ pass = ^^%pass%

它仍然不起作用,我不知道在这一点我失踪了什么。

解决方案

假设你想要字符串 ^& AntiBatchfileString 这是最好的设置语法,因为大多数特殊字符( ^ & > | 以及标准分隔符 ; = SPACE TAB )一旦放在之间,而 本身不成为变量值的一部分:

  setpass = ^& AntiBatchfileString 

只有命令扩展名都可以工作,这是Windows默认的(类型 cmd /?并查看 / E 选项)。



当扩展(读取)变量如%pass%(附带)时,特殊字符但是,一旦扩展它像%pass%(no ),他们恢复了他们的特殊意义。所以你有以下选项:


  1. 使用 setpass = ^^^& AntiBatchfileString,其中 ^^ 转义文字 ^ ^& code>文字& 在阅读时如%pass%

  2. 启用延迟展开(请参阅 set /?关于它的工作原理, setlocal /? cmd /?关于如何启用它),其中变量值在已经完成特殊字符解析的时间点被扩展(读取)。

我喜欢后一种方法,因为不需要特殊的转义,它也可以处理出现在字符串值(即使不对称存在)中。

顺便说一下,也可以通过 ^,只要这不会出现在未转义的中。



然而, code>%符号无法像 ^%在一个批处理文件中,但是您需要像 %% 一样将它们加倍,以获得一个字面值,独立于字符串是否在之间

请注意,在控制台上, %% 不起作用,尽管 ^%



最后,当启用延迟扩展功能时,文字因此,您需要特别注意这些情况,通过像 ^!一样转义,或者通过智能地切换延迟扩展(因此仅在实际需要时启用它)并禁用它,否则,当提供文字字符串时,例如在命令行中,例如,当扩展标准变量,如%pass %,而当<$ code>变量 %% I (批处理文件)或%I (控制台),例如)。当然这也不是最终的解决方案,因为您需要 setlocal endlocal 来启用/禁用延迟扩展,旨在本地化环境变化,所以自 setlocal 命令以来的任何变量都将在$ code> endlocal 之后立即丢失执行(有一些技巧来传递变量值超过 endlocal 屏障)。


Special characters in batch files are a pain, but I haven't found the right workaround for properly escaping the first two characters of this particular string I'm trying to pass the application.

SET pass=^&AntiBatchfileString
A_Program.exe /pass=%pass%

Things I have tried:

:: Escaping the escape twice, first for ^, second for &.
SET pass=^^^^&AntiBatchfileString
echo %pass%

:: Combining escapes.
SET first=^^
SET second=^^&AntiBatchfileString
SET pass=%first%%second%
echo %pass%

:: Preventing expansion
SET first=^^
SET second=^^&AntiBatchfileString
SET pass=!first!%second%
echo %pass%

:: I got this to print correctly
SET "pass=^&AntiBatchfileString"
echo ^^%pass%

Still when passing the last one it doesn't accept the login, I don't know what the final output is. That got me thinking maybe it was trying to do another expansion when passing the parameter to the application, so I quoted that as well.

SET "pass=^&AntiBatchfileString"
A_Program.exe "/pass=^^%pass%"

It's still not working, I'm not sure what I'm missing at this point.

解决方案

Supposing you want the string ^&AntiBatchfileString literally, this is the best set syntax, as most special characters (^ & ( ) < > | and also the standard delimiters , ; = SPACE TAB) lose their particular meaning as soon as ther are placed in between "", and the "" themselves do not become part of the variable value:

set "pass=^&AntiBatchfileString"

This works only as long as the command extensions are on, which is the Windows default anyway (type cmd /? and see the /E option).

When expanding (reading) a variable like "%pass%" (with enclosing ""), special characters are still treated literally.

However, as soon as you expand it like %pass% (no ""), they get back their special meaning. So you have the following options:

  1. Use set "pass=^^^&AntiBatchfileString", where ^^ escapes the literal ^ and ^& the literal & when reading like %pass%.
  2. Enable delayed expansion (see set /? about how it works and setlocal /? or cmd /? about how to enable it), where the variable value is expanded (read) at a point of time where parsing of special characters has already been completed.

I prefer the latter approach, because no special escaping is necessary, and it can also deal with " appearing in the string value (even if unsymmetrically present).
By the way, " can also be escaped by ^", as long as this does not appear within unescaped "".

Nevertheless, % signs cannot be escaped like ^% in a batch file, but you need to double them like %% to get one literal one each, independent whether or not the string is in between "".
Note that on the console, %% does not work although ^% does.

Finally, literal ! are consumed by the delayed expansion feature when enabled, therefore you need to pay particular attention to those in case, by escaping them like ^!, or also by intelligently toggling delayed expansion (hence to enable it only when it is actually needed and to disable it otherwise, when a literal string is provided, like in a set command line, for instance, when expanding a standard variable like %pass% and when reading a for variable like %%I (batch file) or %I (console), for example). Of course this is also not the ultimate solution, because you need setlocal and endlocal to enable/disable delayed expansion, which are intended to localise environment changes, so any variable changes since the most recent setlocal command are lost as soon as endlocal is executed (there are some tricks for passing a variable value over the endlocal barrier though).

这篇关于批处理文件中的特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆