简体   繁体   English

批处理文件中的特殊字符

[英]Special Characters in Batch File

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:假设您想要的字符串^&AntiBatchfileString从字面上看,这是最好的set语法,因为大多数特殊字符( ^ & ( ) < > | ,也是标准的分隔符, ; = SPACE TAB)失去其特定的含义,只要疗法被放置在在""""之间,它们本身不会成为变量值的一部分:

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).这仅在命令扩展打开时才有效,无论如何这是 Windows 默认的(键入cmd /?并查看/E选项)。

When expanding (reading) a variable like "%pass%" (with enclosing "" ), special characters are still treated literally.当扩展(读取)像"%pass%" (用封闭的"" )这样的变量时,特殊字符仍然按字面处理。

However, as soon as you expand it like %pass% (no "" ), they get back their special meaning.然而,一旦你像%pass% (没有"" )一样扩展它,它们就会恢复它们的特殊含义。 So you have the following options:因此,您有以下选择:

  1. Use set "pass=^^^&AntiBatchfileString" , where ^^ escapes the literal ^ and ^& the literal & when reading like %pass% .使用set "pass=^^^&AntiBatchfileString" ,其中^^转义文字^^&文字&读取时像%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.启用延迟扩展(请参阅set /?了解其工作原理和setlocal /?cmd /?了解如何启用它),在特殊字符解析已经完成的时间点扩展(读取)变量值.

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, because percent expansion happens before escaping, 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.请注意,在控制台上, %%不起作用。

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). ,或者也通过智能切换延迟扩展(因此仅在实际需要时启用它,否则禁用它,当提供文字字符串时,例如在set命令行中,例如,当扩展像%pass%这样的标准变量时%pass%和读取for变量时,例如%%I (批处理文件)或%I (控制台))。 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).当然,这也不是最终的解决方案,因为您需要setlocalendlocal来启用/禁用延迟扩展,它们旨在本地化环境更改,因此一旦执行endlocal自最近的setlocal命令以来的任何变量更改都会丢失(不过,有一些技巧可以将变量值传递到endlocal屏障上)。

here are some notes about "Escaping special characters" which can be used in the Batch file and CMD environment:这里有一些关于“转义特殊字符”的注意事项,可以在批处理文件和 CMD 环境中使用:

https://sodocumentation.net/batch-file/topic/10693/escaping-special-characters https://sodocumentation.net/batch-file/topic/10693/escaping-special-characters

if you want to use % as a string without escaping in a batch file如果你想使用 % 作为字符串而不在批处理文件中转义

like %20 u can use %%%20 git clone "https:// abc.com /D%%%220an"像 %20 你可以使用 %%%20 git clone "https://abc.com /D%%%220an"

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

相关问题 在批处理文件中用%替换特殊字符 - replace special characters with % in batch file 批处理文件:将特殊字符写入文本文件 - Batch File:: Write Special characters to text file 特殊字符导致Windows批处理文件关闭 - Special characters cause the Windows Batch File to close 用于从文件名中删除特殊字符的批处理文件脚本(Windows) - Batch file script to remove special characters from filenames (Windows) 批处理文件:提取具有特殊字符的两个字符串之间的子字符串 - batch file: extract substring between two strings with special characters 为什么Windows批处理文件输出的行数组中缺少特殊字符? - Why are special characters missing in array of lines output by Windows batch file? 批处理文件,用于读取带有特殊字符的txt并替换其中的单词 - Batch file to read a txt with special characters and replace a word in it 如何检查变量是否在批处理文件中包含特殊字符 - how to check if the variable contains special characters in batch file 为什么批处理文件中的特殊字符会打印为问号? - Why do special characters in a batch file print as a question mark? 设置变量时如何在批处理文件中转义特殊字符 - How to escape special characters on batch file when setting variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM