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
2
  • ... do set %%1? syntax is: set var=value Commented May 27, 2015 at 17:12
  • Sorry, I have rewrite it Commented May 27, 2015 at 17:20

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.