Q:

Maximum setlocal recursion level reached in batch

The task is to replace the reference path with some new path in collection of html files. I used the below code for that and it throws the maximum setlocal recursion level reached  error,
@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

Can anyone please let me know how to resolve this error?
Thanks in advance..

A:

First, you should ident your code.
The parenthesis aren't balanced, there are more opened than closed parenthesis.
You call a label/function which is a part of your first FOR /r loop, that will never work.  
Perhaps this is what you want (but I can't even guess what you try withyour code)
@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

After edited your code:
The setlocal/endlocal should be in the same block, in your case you call SETLOCAL for every html file, but only call ENDLOCAL once.
But each SETLOCAL needs an ENDLOCAL
After your comment:
You try to modify a html file with percent expansion, that will fail in many cases, as it's tricky to handle the special characters in an html file like <>&|.
Btw. Your For /f loop to read the file content will fail when a line begins with ].
This one should work
: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

But there is a much simpler solution using the repl.bat tool from dbenham

