title: Maximum setlocal recursion level reached - setlocal
body: <p>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. </p>
  
  <p>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!</p>
  
  <p>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'</p>
  
  <p>The code:</p>
  
  <pre><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....
  </code></pre>
  
  <p>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-}"</p>
  
  <p>Apologies - may be obvious to you guys - but hard to get head round. Thanks</p>