title: Batch File: "Environment Variable Not Defined"
body: <p>I am working with a Java program that calls an external batch file and passes in an array of commands. I have a loop in the batch file that looks like this:</p>
  
  <pre><code>set paramCount=0
  for %%x in (%*) do (
      set /A paramCount+=1
      set list[!paramCount!]=%%x
  )
  </code></pre>
  
  <p>The parameters are a bunch of directories, stored as strings, like this:</p>
  
  <pre><code>String[] commands = {"cmd.exe",
                       "/C",
                       "C:\Users\user\Documents", 
                       "C:\Users\user\Pictures", 
                       "C:\Users\user\Videos"}
  </code></pre>
  
  <p>As you can see, my for loop is supposed to loop through the list of the arguments passed to the batch file (%*) and emulate an array within the script (Since the first two elements in the command array are used to start the command process, that only leaves the directories to be looped through). The program was working just fine until the other day, when I suddenly started getting an error saying the following:</p>
  
  <pre><code>Environment variable list[ not defined
  </code></pre>
  
  <p>I had not made any changes to the batch file at all and it seemed to stop working for no reason. In case it's necessary information, I'm using a process builder to run the process:</p>
  
  <pre><code>ProcessBuilder pb = new ProcessBuilder(commands);
  Process p = pb.start();
  </code></pre>
  
  <p>Supposedly using this syntax for an array in a batch file is okay, so I'm not sure why it doesn't accept it. I appreciate any help you can provide in this matter. I've run into a lot of roadblocks with this program, and while I've been able to solve 90% of them, the remaining 10% have begun to drive me crazy! Thanks!</p>
  
  <p><strong>EDIT:</strong>
  I have re-written the loop and added in some echo commands to make debugging easier. When I run the batch file, though, nothing prints to the screen as a result of the echo but I still get the same error:</p>
  
  <pre><code>@echo off
  setlocal enableDelayedExpansion
  set paramCount=0
  for %%x in (%*) do (
      echo !paramCount!
      echo %%x
      set list[!paramCount!]=%%x
      set /A paramCount=paramCount+1
  )
  </code></pre>
  
  <p>I also forgot to mention that the program works fine when I run the Java from Eclipse; it calls the batch files correctly and all works as expected. I don't get the error until I export the project to a runnable JAR and try running that.</p>
  
  <p><strong>EDIT 2:</strong></p>
  
  <p>After going through my batch file code again (I wrote it a while ago), I found only one line that looks like it might cause this problem. The odd thing is that I used an almost identical code example I found somewhere else to model it, and it worked for a long time without ever giving the error. It's a loop, designed to loop through the elements of the list "array" created in the first loop:</p>
  
  <pre><code>for /F "tokens=2 delims==" %%d in ('set list[') do (
      set /A paramCount+=1
      set _dir=%%d
      set _dir=!_dir:"=!
      if NOT "%%d"=="nul" set "dirs[!paramCount!]=!_dir!"
  )
  </code></pre>
  
  <p>As you can see, the first line has a segment that says <code>set list[</code>, which looks odd to me. However, as I mentioned, it worked fine for quite a while.</p>