批量设置本地EnableDelayedExpansion和数学问题 [英] Batch SetLocal EnableDelayedExpansion and Math problems
问题描述
C:\WINDOWS\system32>SetLocal EnableDelayedExpansion
C:\WINDOWS\system32>set/a Number1=3+9
12
C:\WINDOWS\system32>if !Number1!==9+3 (echo Good) else (echo Bad)
Bad
C:\WINDOWS\system32>if !Number1!==3+9 (echo Good) else (echo Bad)
Bad
C:\WINDOWS\system32>set/a i=9+3
12
C:\WINDOWS\system32>if !Number1!==%i% (echo Good) else (echo Bad)
Bad
我希望看到最后一个结果(可能还有其他一些结果)显示Good
,但没有!我认为这是因为SetLocal EnableDelayedExpansion
错误,但我的代码中需要此错误.因此,如何使用SetLocal EnableDelayedExpansion
获得预期的结果.感谢您提供的任何帮助=)
I expected to see the last results (and maybe some others) to show Good
as a result but did not! I think this is because it is a error with the SetLocal EnableDelayedExpansion
but I need that in my code. So how do I get my expected result with SetLocal EnableDelayedExpansion
. Thanks for any help provided =)
推荐答案
setlocal EnableDelayedExpansion
仅在批处理文件中有效(另请参见setlocal /?
),在命令提示符下键入时无效.因此!!
扩展无法正常工作.
setlocal EnableDelayedExpansion
only works within batch files (see also setlocal /?
), it has no effect when being typed into command prompt; therefore the !!
expansion does not work.
要在命令提示符下使用延迟扩展,您需要打开一个新的cmd
实例:
To use delayed expansion in command prompt, you need to open a new cmd
instance:
cmd /V:ON
您不能直接在if
语句的比较表达式中进行算术运算,您需要预先进行所有计算.
此外,您应使用比较运算符EQU
进行数字运算,因为==
会强制进行字符串比较:
You cannot do arithmetics in the comparison expressions of the if
statement directly, you need to do all the calculations in advance.
In addition, you should use the comparison operator EQU
for numeric operations, because ==
forces string comparison:
set /A Number1=3+9
set /A i=9+3
if !Number1! EQU %i% (echo Good) else (echo Bad)
这篇关于批量设置本地EnableDelayedExpansion和数学问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!