0

I have a .bat file with the following commands.

My aim is concatenate the CLASSPATH variable value with the values of %%1.

The three first commands (outside of FOR command) concatenate the CLASSPATH correctly, but the FOR command only do it with the last value of the iteration.

Please help

    SET CLASSPATH=%CLASSPATH%;test1.jar
    SET CLASSPATH=%CLASSPATH%;test2.jar
    SET CLASSPATH=%CLASSPATH%;test3.jar

    FOR %%1 IN ("%CXFHOME%\lib\*.jar") DO SET CLASSPATH=%CLASSPATH%;%%1
josepmra
  • 577
  • 8
  • 25

2 Answers2

4

You need delayed expansion:

setlocal enabledelayedexpansion
FOR %%1 IN ("%CXFHOME%\lib\*.jar") DO SET CLASSPATH=!CLASSPATH!;%%1
echo %classpath:~1%

See here for explanation and demonstration of delayed expansion.

Community
  • 1
  • 1
Stephan
  • 50,835
  • 10
  • 55
  • 88
1

use setlocal enabledelayedexpansion in your script , the reason why concatenation is happening for last value of for loop is , for loop is taken as single instruction at run time so , all the values are replaced and the last values is only reflected.

prudviraj
  • 3,454
  • 2
  • 14
  • 21