Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maximum setlocal recursion level reached - setlocal

Read a few bits, but struggling to get my head around this. Basically, having to use Setlocal with enable and disable delayed expansion to use and manipulate variables with exclamation marks in. Example of this code below. The code section can be cycled through many times in a session, and at some point (I believe it's 32?) I get a "Maximum setlocal recursion level reached" error.

I've read a little about Endlocal, but can't get my head around how to use it. My worry is that some parts of the code extract (below) use environment vars from the surrounding code (e.g. %_emuExts%) whilst there are some vars set in the extract that I want to use outside the extract (e.g. %launchfile% and %lauchfile2%). Hope that makes some kinda sense!

My question is really around where to put any EndLocal to ensure that can use variables from 'around' the code and also set variables that can be used outside of the 'local' bit. To be honest - struggling to conceptualize the ideas around 'local' and 'recursion levels'

The code:

:: Example of var contents:
set _emuExts=cue bin iso
:: Problem code:
Setlocal DisableDelayedExpansion
for %%e in (%_emuExts%) do (
        echo Extention: %%e 
        for /F "delims=" %%a in ('dir /b *.%%e') do (               
                set launchfile=%%~na.%%e            
                echo Launchfile this iteration: [%%~na.%%e]%_log1%
            )
        echo Next iteration......
    )
echo Final Launchfile: "%launchfile%" %_log1%
set launchfile2=%launchfile:!={-exc-}%
echo Launchfile with replace: %launchfile2%%_log1%
setlocal enabledelayedexpansion
:: .....more code   
Setlocal DisableDelayedExpansion
set "_FinalemuCmd=%_FinalemuCmd:{-exc-}=!%"
start /wait "" "%_emuExe%" %_FinalemuCmd%
setlocal enabledelayedexpansion
:: ...continues....

Functionally, this cycles through the present dir via the extentions specified in %_emuExts% and sets %launchfile% to the 'priority' file extension (rightmost extension). Some filenames have exclamation marks in - hence the delayed expansion switching. It then switches out "!" for "{-exc-}"

Apologies - may be obvious to you guys - but hard to get head round. Thanks

like image 817
stigzler Avatar asked Oct 29 '25 15:10

stigzler


1 Answers

Rather than complicating the code with all the SETLOCALs, can the code be modified so that you don't need to to enable delayed expansion? I don't see any code above that requires it. Granted there is code missing. Maybe we should look at that. If you must SETLOCAL numerous times,there is a technique called 'tunneling' that allows you to pass variables that are local in one section of code to another section of code. To do that you can do something like thisendlocal & set MyVar=%MyVar%. This works because at load time the entire line is parsed and %MyVar% is expanded. The run time behavior is to then set that value into a new variable (of same or different name, as you choose) outside the localized code.

like image 61
RGuggisberg Avatar answered Oct 31 '25 10:10

RGuggisberg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!