【发布时间】:2013-12-02 22:40:00
【问题描述】:
为什么字符串操作在 if 语句中内联工作而不使用延迟扩展变量 - 但否则会失败。例如:
set test=testString
if %test:~0,4%==test echo Success
这可以正常工作;返回Success。但是,如果我执行以下操作:
setLocal enableDelayedExpansion
set test=testString
if !test:~0,4!==test echo Success
我收到错误 - 4!==test was unexpected at this time.
显然,您可以通过执行 set comp=!test:~0,4! 之类的操作来解决此问题,然后只需在 if 语句中使用 !comp! 变量即可。
【问题讨论】:
-
if "!test:~0,4!" equ "test" echo Success- 以这种方式工作,但这当然不是答案......
标签: batch-file if-statement cmd delayedvariableexpansion