China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-24 11:25
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Regarding the maximum recursion depth of setlocal View 4,639 Replies 5
Original Poster Posted 2006-06-17 20:40 ·  中国 广东 广州 电信
荣誉版主
★★★
Credits 718
Posts 313
Joined 2005-09-26 00:00
20-year member
UID 42844
Gender Male
Status Offline
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".
Floor 2 Posted 2006-06-18 18:17 ·  中国 山西 运城 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
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.
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 3 Posted 2006-06-19 17:13 ·  中国 北京 联通
银牌会员
★★★
DOS联盟捡破烂的
Credits 1,144
Posts 425
Joined 2005-10-20 00:00
20-year member
UID 43784
From 北京
Status Offline
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!!!!!!!!!
Floor 4 Posted 2006-06-20 13:03 ·  中国 广东 广州 越秀区 电信
荣誉版主
★★★
Credits 718
Posts 313
Joined 2005-09-26 00:00
20-year member
UID 42844
Gender Male
Status Offline
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.
Floor 5 Posted 2006-06-20 14:11 ·  中国 山西 大同 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
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 ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
everest79 +8 2007-03-29 21:07
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 6 Posted 2007-05-25 22:56 ·  中国 辽宁 大连 联通
初级用户
Credits 70
Posts 38
Joined 2007-03-24 09:25
19-year member
UID 82762
Gender Male
Status Offline
:: 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...
Forum Jump: