Windows用户定义的环境variables名称可以包含任何字符,除了= 。
特殊字符可以通过转义来包含。 一个简单的方法是简单地将整个SETexpression式括在引号中。 例如:
set "A weird & "complex" variable=My value" set A weird ^& "complex" variable=My value
上面的两个expression式都给出相同的结果 variables名称是A weird & "complex" variable ,值是My value
IF DEFINED结构用于testingvariables是否被定义。 行情不适用于此testing,名称(包括引号)中的特殊字符必须转义。
lftp + bash脚本+variables
本地variables声明放置需要Microsoft C / C ++编译器开关
批处理 – 将variables的命令输出分配给variables
将参数从Windows批处理脚本传递到PowerShell脚本
set "A&B=value" if defined A^&B echo This works if defined "A&B" echo This does not work
上面的逃脱testing工作得很好。 引用的testing不起作用
但是我怎样才能testing一个包含空格的variables是否存在?
set "AB=value" if defined A^ B echo this does not work!
似乎上面应该工作,但它不!
我正在寻找一个不涉及使用%AB%或!AB扩展variables的答案!
BATCH-与二进制
在包含开关中使用variables时,Get-ChildItem失败
如何在Windows上为Mercurial指定一个不同的编辑器?
如何在Windows上调用.bat文件并在一行中设置本地环境variables?
Windows问题的环境variables
交流的问题(我爱这个语法基础问题)。
显然你知道如何检查延迟扩展和FOR参数的作品。
@echo off setlocal set "AAA BBB=value" set ""AAA BBB"=" set "AAA=" for %%a in ("AAA BBB") do if defined %%~a echo FOR: This works setlocal EnableDelayedExpansion set "varname=AAA BBB" if defined !varname! echo Delayed: This works if defined %varname% ( echo percent: Never comes here ) ELSE ( echo percent: Never comes here ? ) if defined AAA^ BBB ( echo escape1: Never comes here ) ELSE ( echo escape1: fails ) set AAA=Hello if defined AAA^ BBB ( echo escape2: It only test for AAA the BBB will be "removed" ) ELSE ( echo escape2: fails ) set "space= " if defined AAA!space!BBB echo inject space: This works if defined "AAA BBB" (echo Quote1: Never comes here ) ELSE ( echo Quote1: Fails ) set ""AAA BBB"=value" if defined "AAA BBB" echo Quote2: This works,it checks for "AAA BBB" with quotes
在我的opionion中,在escape2的例子中,解析器首先将这一行分割为标记:
<if> <defined> <AAA BBB> <echo ....但是在if定义的执行时间重新扫描<AAA BBB>令牌,所以它只获取AAA 。
你不能注入像AAA^^^ BBB这样的第二个转义,因为这只能搜索名为AAA^的变量
编辑:它也可以用SET <varname>来解决
ijprest的解决方案使用SET命令来测试变量,而不需要转义varname。
但是它也显示了在varname内部和末尾处的空间的inteRSSting行为。
它似乎遵循这些规则:
SET varname搜索以varname开头的所有变量,但首先删除varname的最后一个空格字符后面的所有字符,并删除所有前导空格。
所以你不能搜索以空格开头的变量(但是创建这样的varname也有点棘手)。
如果将变量名用引号括起来,那么同样的行为也是有效的,但是之后又存在一个规则。
如果至少有两个引号,请先删除最后一个引号后的所有字符。 使用引号内的文本,并使用“空格” – 规则。
样品。
set " abc def ghi" junk junk *** 1. removes the junk set " abc def ghi" *** 2. removes the quotes set abc def ghi *** 3. removes all after the last space,and the trailing spaces set abc def *** Search all variables beginning with abc def
我也喜欢这样的问题!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。