Re 220110:
测试证明setlocal的最大递归层数为32。
事实上,我们很少需要在代码内部频繁setlocal或者开关变量延迟和命令扩展,我通常在需要变量延迟时直接使用 @echo off & setlocal EnableDelayedExpansion 。
如果确需在for中使用setlocal,那么切记在同一层for内使用相对应的endlocal结束setlocal的影响。
你的问题既然不是setlocal写在for中,则可能是出在多次goto到含有 setlocal 的标签段了;但既然问题无法重现,这些就只能是猜测了。
:: SetlocalMaxRecursion.cmd - Test of max recursion of setlocal
:: Will Sort - 2006-06-20 - CMD@WinXP
@echo off
for /l %%i in (1,1,100) do (
setlocal EnableDelayedExpansion
rem echo.%errorlevel%
set var=%%i
echo counter:%%i-!var!
if "!var:~-1!"=="0" pause
endlocal
rem 使用rem注释endlocal可以测得setlocal的最大递归层数为32
rem 这里的注释标记不能使用::,否则可能会产生语法错误
echo counter:%%i-!var!
rem endlocal也关闭最近的setlocal所设定的变量延迟或者命令扩展
)
Last edited by willsort on 2006-6-20 at 14:21 ]
Re 220110:
Testing proves that the maximum recursion level of setlocal is 32.
In fact, we rarely need to frequently use setlocal or switch variable delay and command extensions inside the code. I usually directly use @echo off & setlocal EnableDelayedExpansion when variable delay is needed.
If setlocal is indeed needed in a for loop, then be sure to use the corresponding endlocal within the same layer of the for loop to end the influence of setlocal.
Since your problem is not about setlocal written in a for loop, it may be due to multiple gos to label segments containing setlocal; but since the problem cannot be reproduced, these can only be guesses.
:: SetlocalMaxRecursion.cmd - Test of max recursion of setlocal
:: Will Sort - 2006-06-20 - CMD@WinXP
@echo off
for /l %%i in (1,1,100) do (
setlocal EnableDelayedExpansion
rem echo.%errorlevel%
set var=%%i
echo counter:%%i-!var!
if "!var:~-1!"=="0" pause
endlocal
rem Using rem comments for endlocal can measure that the maximum recursion level of setlocal is 32
rem The comment mark here cannot use ::, otherwise a syntax error may occur
echo counter:%%i-!var!
rem endlocal also closes the variable delay or command extension set by the nearest setlocal
)
Last edited by willsort on 2006-6-20 at 14:21 ]