windows batch command for loop set environment variable

匿名 (未验证) 提交于 2019-12-03 02:42:02

问题:

I'm trying to find a way to dynamically build en environment variable that contains a list of JAR files under my app's WEB-INF/lib folder.

This is what I have so far, but it's over-writing itself each time, so when you reach the end of the loop, you just have the last file from the loop set in the variable.

 SETLOCAL ENABLEDELAYEDEXPANSION  for %%f IN (WEB-INF/lib/*.jar) DO (    SET JAR_LIST=%JAR_LIST%;%%f  )  ECHO JAR_LIST -- %JAR_LIST% 

So this produces...

C:\apache\Tomcat6.0\webapps\myapp>(SET JAR_LIST=.;xsltcbrazil.jar )

C:\apache\Tomcat6.0\webapps\myapp>(SET JAR_LIST=.;xsltcejb.jar )

C:\apache\Tomcat6.0\webapps\myapp>(SET JAR_LIST=.;xsltcservlet.jar )

C:\apache\Tomcat6.0\webapps\myapp>ECHO JAR_LIST -- .;xsltcservlet.jar

JAR_LIST -- .;xsltcservlet.jar

回答1:

Change

SET JAR_LIST=%JAR_LIST%;%%f 

to

SET JAR_LIST=!JAR_LIST!;%%f 

This will use the run-time value instead of the load-time value. It might be better to do this to avoid the leading ;

SETLOCAL ENABLEDELAYEDEXPANSION SET "JAR_LIST=" for %%f IN (WEB-INF/lib/*.jar) DO ( if "!JAR_LIST!"=="" (SET JAR_LIST=%%f) ELSE (SET JAR_LIST=!JAR_LIST!;%%f) ) ECHO JAR_LIST -- %JAR_LIST% 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!