4

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:

set paramCount=0
for %%x in (%*) do (
    set /A paramCount+=1
    set list[!paramCount!]=%%x
)

The parameters are a bunch of directories, stored as strings, like this:

String[] commands = {"cmd.exe",
                     "/C",
                     "C:\Users\user\Documents", 
                     "C:\Users\user\Pictures", 
                     "C:\Users\user\Videos"}

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:

Environment variable list[ not defined

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:

ProcessBuilder pb = new ProcessBuilder(commands);
Process p = pb.start();

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!

EDIT: 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:

@echo off
setlocal enableDelayedExpansion
set paramCount=0
for %%x in (%*) do (
    echo !paramCount!
    echo %%x
    set list[!paramCount!]=%%x
    set /A paramCount=paramCount+1
)

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.

EDIT 2:

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:

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!"
)

As you can see, the first line has a segment that says set list[, which looks odd to me. However, as I mentioned, it worked fine for quite a while.

DerStrom8
  • 1,311
  • 2
  • 23
  • 45
  • [This answer](http://stackoverflow.com/a/10167990/180100) might help –  Dec 19 '13 at 21:36
  • Thank you for the link. Unfortunately I'm not quite sure it helps me with this particular issue. I am using delayed expansion to ensure the !!s work. I ensure %...%s and !...!s are used properly, and in the correct order. The issue appears to have something to do with paramCount, as it seems to only read the list[ part of the variable instead of the full thing ("list[]"). Not quite sure what would cause this. – DerStrom8 Dec 19 '13 at 22:07
  • did you use setlocal enabledelayedexpansion ? also paramCount+=1 is better to be paramCount=!paramCount!+1 – mihai_mandis Dec 19 '13 at 22:46
  • @mihai_mandis Yes, it is the first line in my batch file, other than an echo off statement. As for setting paramCount, I was not aware there was a difference. Does += cause problems in a batch file? – DerStrom8 Dec 19 '13 at 22:48
  • should not cause problems, but...I just want to make it clear. Please see also some problems when using %* here http://stackoverflow.com/questions/20617065/is-this-a-bug-in-windows-batch/20617411?noredirect=1#comment30861213_20617411 – mihai_mandis Dec 19 '13 at 22:56
  • What is really confusing me at this point is why it decided to stop taking it so suddenly. I didn't make any changes at all. Something I also forgot to mention in the original post is that it works fine when I run from Eclipse. It's just when I export the project to a runnable JAR that it gives me that error. – DerStrom8 Dec 19 '13 at 23:05
  • @mihai_mandis - There is absolutely nothing wrong with `set /a parmCount+=1`. It cannot be the source of the problem, and I would argue it is the preferred way to increment a value, but it is mostly a matter of style. – dbenham Dec 19 '13 at 23:16
  • @derstrom8 - perhaps there is something unusual about the parameters that are being passed in that eventually induces the error. Presumably the parameter values are dynamic. – dbenham Dec 19 '13 at 23:18
  • @dbenham you are correct, they are dynamic. They are taken from a user input through java code, and then passed in to the batch file as arguments. I can't imagine it's a problem with the directories, as a) The same directories worked at first, and b) no special characters ("s, for example) are allowed for file/directory names. – DerStrom8 Dec 19 '13 at 23:36

3 Answers3

1

The code you posted cannot give that error message. There must be some other code in your script that causes that error.

Only a SET statement without an = could give that error. After parsing and expansion, the offending statement must look like one of the following:

set list[
set "list[
set "list["

Actually, you could get that error in the presence of an = if there are quotes that are misplaced. For example, the following will give that error because all text after the last " is ignored:

set "list["1]=value

The 1]=value appears after the last " and is ignored, leaving set "list[".

You might consider enabling ECHO so that you can pinpoint exactly where the error message is occurring. Then you need to figure out what conditions could lead to the error at that point.


Update in response to "Edit 2:" in question

That IN() clause in the newly posted code is almost assuredly the point where the error message is generated. It indicates that the list "array" is not defined when the FOR /F loop is executed. The question is, why not? Things to look for:

  • Is something preventing the earlier FOR loop that defines the list "array" from running?

  • Are you sure the parameters are getting passed properly to the script? If there are no parameters, then there will not be an array.

  • Is something undefining the array before the 2nd FOR loop has a chance to execute?

I suggest you put in some diagnostic ECHO statements to help debug. For example, ECHO BEFORE 1ST LOOP immediately before the loop that defines the array to make sure the loop is being reached. Or ECHO ARGS = %* at the beginning of the script to make sure the parameters are getting passed properly. Happy sleuthing :-)

dbenham
  • 127,446
  • 28
  • 251
  • 390
  • Strangely, removing the @echo off statement did not help at all. Absolutely nothing appears in the console window except for that error message. As for your other suggestions, I have gone over my code very carefully and I do not see anything out of place. However, I do see one statement that doesn't look right, but I had used the same syntax as another source and it worked for a long time. It's another for loop, where it loops through the list "array" created in the loop I already posted. I will add that to the original post in a second edit. – DerStrom8 Dec 19 '13 at 23:29
  • Here is the thread I used for reference to write the loop: http://stackoverflow.com/a/18480952/2990189 – DerStrom8 Dec 20 '13 at 00:35
  • in response to your update, I placed an echo at the very start of the program (echo testing) but the error appeared immediately without ever printing "testing" to the console. I have checked the Java code to ensure all the commands are correct when they're passed in to the process builder. I also tried echo %* with no luck. The error appears right away. If that was working I'm sure I could have narrowed it down by now, since I tried placing echo debug statements in the code in various areas. None of them were printed, however, which is why I posted the question here. – DerStrom8 Dec 20 '13 at 22:04
  • Okay, please excuse my stupidity. I had completely forgotten that I had made an exe from the old bat file, and THAT is what I have been running--the exe. So every time I edit the bat file, the exe remains the same. I am going to do some more "sleuthing", but this time using the batch file for testing =P – DerStrom8 Dec 20 '13 at 22:40
0

I tried the code bellow and it worked for me when calling from command line.

@echo off
setlocal enabledelayedexpansion

set paramCount=0

for %%x in (%*) do (
    set /A paramCount=!paramCount!+1
    set list[!paramCount!]=%%x
)
echo !list[1]!
echo !list[2]!
echo !list[3]!
mihai_mandis
  • 1,578
  • 1
  • 10
  • 13
  • Unfortunately I am still having issues. I have re-arranged some of the code in the loop and added in some "echo" commands to see what's happening. I've posted the code in an edit to my original. – DerStrom8 Dec 19 '13 at 23:03
0

I mentioned it in the comments but I figured I'd post an answer, for anyone else who's wondering:

As previously mentioned, the program was running fine in Eclipse, the batch files were being called and ran as expected. However, prior to making some changes to the batch files, I had created .exe's from them, and apparently was running the .exe's instad of the .bat's. It was a very stupid mistake on my part, and the problem was caused by some errors in the previous version of the batch file. If I remember correctly, that error was because the "array element" was empty. So I ended up doing a check to make sure it wasn't empty before operating on it. The following is the code i am currently using, and it works as expected:

@echo off
setlocal ENABLEDELAYEDEXPANSION
set /A paramCount=-3
for %%x in (%*) do (
    set list[!paramCount!]=%%x
    set /A paramCount=paramCount+1
)
set argA=%list[-3]%
set argB=%list[-2]%
set argC=%list[-1]%
for /F "tokens=2 delims==" %%a in ('set list[-') do SET "%%a="
set list[-3]=""
set list[-2]=""
set list[-1]=""
set paramCount=0
for /F "tokens=2 delims==" %%d in ('set list[') do (
    if not "%%d"=="" (
        set _dir=%%d
        set _dir=!_dir:"=!
        if NOT "%%d"=="nul" set "dirs[!paramCount!]=!_dir!" 
    ) 
    set /A paramCount+=1 )

Thanks for all the answers, folks!

DerStrom8
  • 1,311
  • 2
  • 23
  • 45