Package and for loop

I have a java application running a .cmd file. I want to set the path to the application class through this batch, all the necessary banks are placed in the lib folder.

Here is what I tried:

set _classpath=. for %%i in (%1/lib/*.*) do ( set _classpath=%_classpath%;%%i ) 

Surprisingly, it does not seem to act as expected. Let's say there are 3 jar in the lib folder:

  • pikachu.jar
  • sonic.jar
  • mario.jar

Here's what happens:

  • set _classpath =.
  • set _classpath =; pikachu.jar
  • set _classpath =; sonic.jar
  • set _classpath =; mario.jar

Obviously i want to get

  • set _classpath = .; pikachu.jar; sonic.jar; mario.jar

Any idea?

Thank you and welcome

+6
java classpath batch-file
source share
2 answers

Put this at the top of your batch file:

 setlocal enabledelayedexpansion 

Then inside the for loop replace %_classpath% with !_classpath!

Without deferred extension, %_classpath% expands once, at the beginning of the for loop.


[Edit] In response to the comment here is a complete list of codes

 @echo off setlocal enabledelayedexpansion set _classpath=. for %%i in (%1/lib/*.*) do ( set _classpath=!_classpath!;%%i ) echo %_classpath% pause 
+4
source share

CMD.EXE extends %...% to start the loop.

You need slow expansion of a variable, is this explained in set /? from the command line.

+1
source share

All Articles