title: %%G from For Loop changing from index to %G using
body: <p>I am trying to write a batch file to automate some routine Matlab processes. The batch file loops through from 0 to a set value (usually between 50 and 75) using the For /L structure. The script copies the main Matlab script to the subfolder and runs it. The batch would normally continue onward so I put a :loop to wait until the Matlab ends. </p>
  
  <pre><code>echo off
  setlocal EnableDelayedExpansion
  
  REM The format is matlab_auto.in (max value). 
  For /L %%G in (0,1,%1) do (
  REM Sanity check
  echo 1 %%G
  
  REM Create Outputs folder if non-existent
  if not exist Outputs md Outputs
  
  REM Copy .m file into deg folder and cd to folder
  copy values_calc.m %%Gsort\values_calc.m
  cd %%Gsort
  echo Got to folder
  
      REM Running .m script and sanity check
  echo 2 %%G
  matlab -nosplash -nodesktop -noFigureWindows -logfile output.log -r "run('values_calc.m');"
  echo 3 %%G
  
  REM Waiting for matlab to finish
  :loop
  tasklist /fi "imagename eq MATLAB.exe" |find ":" &gt; nul
  echo 4 %%G
  if errorlevel 1 goto loop
  echo Finished Matlab  
  echo 5 %%G
  
  REM Copy .m outputs into outputs folder, ignoring confirmation
  copy Output_*.* ..\Outputs /Y
  echo Copied outputs 
  
  REM Sanity check and return home
  echo 6 %%G
  cd %~dp0
  echo Home again
  </code></pre>
  
  <p>)</p>
  
  <p>The problem I'm having is that when it ends after the first iteration of the For loop. Echo 1, 2, 3 are 0. Echo 4 shows 0 the first time through :loop but then it shows %G for the remainder of the loops and at Echos 5 and 6. It also does not continue into further iterations of the For loop. I'm assuming this is because %%G is no longer a number (or in the range specified). </p>
  
  <p>I have tried implementing a call subroutine to use the goto outside the loop but then it opens the Matlab dozens of times, crashing the computer. </p>
  
  <p>Any insight or advice is appreciated. Thank you.</p>
  
  <p>EDIT: Changed the :: for commenting to REM. It did not resolve this issue but looks better.  </p>
  
  <p>EDIT 2: I have a test case that demonstrates the problem. Its something with the :loop or goto. 
      echo off
      setlocal EnableDelayedExpansion</p>
  
  <pre><code>for /l %%G in (0,1,5) do (
      :loop
      echo %%G
      pause
      if %%G==0 goto loop
  )
  </code></pre>