【问题标题】:Maximum setlocal recursion level reached in batch批量达到的最大 setlocal 递归级别
【发布时间】:2015-01-08 13:35:27
【问题描述】:

任务是用 html 文件集合中的一些新路径替换引用路径。我为此使用了下面的代码,它抛出了最大 setlocal 递归级别达到错误,

@echo off
for /r ".\" %%f in (\html\*.htm) do (
    SETLOCAL
    call :SUB ../icons ../../icons "%%f">"%%f_new" 

    del "%%f"
)
for /r ".\" %%f in (*.htm_new) do rename "%%f" "*.htm"
ENDLOCAL
exit /b

:SUB
call
if "%~1"=="" findstr "^::" "%~f0"&GOTO:EOF
for /f "tokens=1,* delims=]" %%A in ('"type %3|find /n /v """') do (
    set "line=%%B"
    call set "line=echo.%%line:%~1=%~2%%"
    for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X
)
exit /b

谁能告诉我如何解决这个错误?

提前谢谢..

【问题讨论】:

    标签: batch-file


    【解决方案1】:

    首先,您应该识别您的代码。
    括号不平衡,开括号比闭括号多。

    您调用一个标签/函数,它是您的第一个 FOR /r 循环的一部分,它永远不会起作用。

    也许这就是你想要的(但我什至无法猜测你用你的代码尝试了什么)

    @echo off
    for /r ".\" %%f in (\html\*.htm) do (
        SETLOCAL
        call :SUB ../icons ../../icons "%%f">"%%f_new" 
        del "%%f"
        ENDLOCAL
    )
    for /r ".\" %%f in (*.htm_new) do rename "%%f" "*.htm"
    exit /b
    
    :SUB
    if "%~1"=="" findstr "^::" "%~f0"&GOTO:EOF
    for /f "tokens=1,* delims=]" %%A in ('"type %3|find /n /v """') do (
            set "line=%%B"
            call set "line=echo.%%line:%~1=%~2%%"
            for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X
    )
    exit /b
    

    编辑您的代码后:
    setlocal/endlocal 应该在同一个块中,在您的情况下,您为每个 html 文件调用 SETLOCAL,但只调用一次 ENDLOCAL。 但是每个SETLOCAL 都需要一个ENDLOCAL

    在您发表评论后:
    您尝试使用百分比扩展修改 html 文件,但在许多情况下会失败,因为处理 html 文件中的特殊字符(如 <>&|)很棘手。

    顺便说一句。当行以] 开头时,您的For /f 循环读取文件内容将失败。

    这个应该可以的

    :SUB
    if "%~1"=="" findstr "^::" "%~f0"&GOTO:EOF
    setlocal DisableDelayedExpansion
    for /f "tokens=* delims=" %%A in ('"type %3|find /n /v """') do (
            set "line=%%B"        
            setlocal EnableDelayedExpansion
            set "line=!line:%~1=%~2!"
            set "line=!line:*]=!"
            echo(!line!
            endlocal
    )
    exit /b
    

    但是使用 dbenham 的 repl.bat 工具有一个更简单的解决方案

    【讨论】:

    • 对不起,我的代码有括号,但我想在这里添加。现在我编辑了我的代码。而且我在两端都添加了 exit /b 。我仍然遇到同样的错误。请做必要的事情
    • '・ソ
    • 我在 forloop 上方设置了 setlocal,现在它可以正常工作了。感谢您的宝贵回答...
    猜你喜欢
    • 2018-10-04
    • 1970-01-01
    • 2012-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-20
    相关资源
    最近更新 更多