views:

492

answers:

1

I need to set my classpath using all of the jars in a particular directory. Bash does it as follows:

CP_DELIMITER=;
for j in "$MY_HOME/javalib/*.jar"; do
    if [ "$CP" ]; then
        CP="$CP$CP_DELIMITER$j"
    else
        CP="$j"
    fi
done

But "for" works differently in DOS, and essentially sends the command to the shell, but won't preserve the "set" on the variable

set CP=./
for %%j in (%MY_HOME%\javalib\*.jar) do (
    set $CP=%CP%;"%%j"
)

When you ask for $CP outside the for, you only get the last jar file. If you echo inside, you can see that %%j does have all of the values.

Has anyone found a solution?

+2  A: 

You'll need to enable delayed environment variable expansion with CMD.EXE /V and use !VAR!:

set CP=./
for %%j in (%MY_HOME%\javalib*.jar) do ( set CP=!CP!;"%%j" )
Zach Scrivena
Great, that works, and an alternative to /V on the command line is:SETLOCAL ENABLEDELAYEDEXPANSION
oniseijin