Jason S
Jason S

Reputation: 189686

win32 batch files: gather and concatenate all matching files

In a Windows batch file, I want to construct a classpath of .jar files in a trusted directory.

I thought this might work:

set TMPCLASSPATH=
for %%J in (*.jar) do set TMPCLASSPATH=%TMPCLASSPATH%;%%J

This doesn't seem to work, since %TMPCLASSPATH% appears to be evaluated once at the beginning of the for loop.

Any suggestions?

Upvotes: 0

Views: 174

Answers (1)

John Knoeller
John Knoeller

Reputation: 34148

You need to use delayed expansion, you add SETLOCAL ENABLEDELAYEDEXPANSION to the top your batch file, and use ! rather than % around the variable names.

SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
set TMPCLASSPATH=
for %%j IN (*.jar) DO set TMPCLASSPATH=!TMPCLASSPATH!;%%j
echo %TMPCLASSPATH%

Upvotes: 2

Related Questions