title: Batch: How to set variable in a nested for-loop and re-use it outside of it (enabledelayedexpansion does not work)
body: <p>I have a batch inside a folder whose goal is to execute all the batch files located in its sub-folders and evaluate their <em>errorlevel</em>. If at least one of them is equal to 1, the main batch will return an exit code equal to 1. This does not mean that the main batch have to exit with the first errorlevel equal to 1: all the sub-batch files must be executed anyway.</p>
  <p>EDIT: all the sub-batch files return an exit code equal to 1 if they fail or equal to 0 if they pass (they are all batch files for testing purpose I wrote myself).</p>
  <p>Problem: the <em>exit_code</em> variable never changes outside the loops.</p>
  <p>I found similar problems here on SO (and a very similar one: <a href="https://stackoverflow.com/questions/7522740/counting-in-a-for-loop-using-windows-batch-script">Counting in a FOR loop using Windows Batch script</a>) but they didn't help (probably I didn't understand... I don't know, I'm new to batch scripting).</p>
  <p>Thanks in advance.</p>
  <p>CODE:</p>
  <pre><code>@echo off
  setlocal enabledelayedexpansion
  set exit_code=0
  for /D %%s in (.\*) do (
      echo ********************  %%~ns  ********************
      echo.
      cd %%s
      for %%f in (.\*.bat) do (
          echo calling %%f
          call %%f
          if errorlevel 1 (
              set exit_code=1
          )
      )
      echo.
      echo.
      cd ..   
  )
  
  echo !exit_code!
  
  exit /B !exit_code!
  </code></pre>