中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-10 16:53
47,811 topics / 349,897 posts / today 0 new / 48,256 members
DOS批处理 & 脚本技术(批处理室) » Regarding the maximum recursion depth of setlocal
Printable Version  4,773 / 5
Floor1 220110 Posted 2006-06-17 20:40
荣誉版主 Posts 313 Credits 718
I tried to use a three-level for loop for iteration, and during the running process, it kept "cursing": "The maximum setlocal recursion level has been reached." What does this mean? How large is the maximum?

Note: --- We call the warning of compilation errors or runtime errors "cursing".
Floor2 willsort Posted 2006-06-18 18:17
元老会员 Posts 1,512 Credits 4,432
Re 220110:

It is best to post the source code for analysis. Initially, it is guessed that setlocal is written inside the for loop without a corresponding endlocal.
Floor3 bagpipe Posted 2006-06-19 17:13
银牌会员 Posts 425 Credits 1,144 From 北京
SETLOCAL has a limit on recursion levels. If you repeatedly enable and disable the delayed environment variable option, it will cause a recursion level error problem, and the specific problem remains to be solved!!!!!!!!!
Floor4 220110 Posted 2006-06-20 13:03
荣誉版主 Posts 313 Credits 718
Note: My code is generally in this format:


@echo off
setlocal
:main
....
....
....
:endofmain
:eof
endlocal


I don't write setlocal and endlocal into the code; I also don't enable any variable delay, just enable extension functions.
The original problematic code has been deleted and modified, and now I can't write back to the original, so I can't post it.
Floor5 willsort Posted 2006-06-20 14:11
元老会员 Posts 1,512 Credits 4,432
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.



[ Last edited by willsort on 2006-6-20 at 14:21 ]
Floor6 mtiankong Posted 2007-05-25 22:56
初级用户 Posts 38 Credits 70
:: 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 to endlocal can measure that the maximum recursion level of setlocal is 32
rem The comment mark here cannot use ::, otherwise it may cause a syntax error
echo counter:%%i-!var!
rem endlocal also closes the variable delay or command extension set by the nearest setlocal
)

I tested under XP and went all the way to 100, I don't understand...
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023