在Windows批处理中计数Var

在我的batch file中,我有以下variables:

set collection=collection1 set environment=oracleDev set processChain1=-help,-startimport %environment% %collection% 

正如您所看到的,我的进程链包含两个以“,”分隔的string。

现在我想要计算两个string(以后可能会多一个string)。 我尝试过:

 Set count=0 For %%j in (%%processChain1%%) Do Set /A count+=1 echo %count% 

但是有第一个错误。 它打印出1而不是2。为什么?

在计算string之后,我想用每个参数(来自variablesprocessChain1的string)启动一个应用程序,我试着用:

 FOR /L %%G IN (1,1,%count%) DO ( FOR /F "tokens=%count% delims=," %%H IN ("%processChain1%") DO java -jar App.jar %%H ) 

现在这个不能正确的工作,因为第一个错误,柜台是错的。 但我认为如果我能解决第一个问题,第二个问题应该可以正常工作。 它是否正确?

Solutions Collecting From Web of "在Windows批处理中计数Var"

据我所知,现在正在计数1,因为该var中只有一个字符串,您稍后将进行拆分,但您的令牌计数已经设置为1 ….

你需要分割第一个字符串(delims =,),然后在第二个部分,处理每个结果。

编辑

尝试这个…

 @echo off set collection=collection1 set environment=oracleDev set processChain1="-help" "-startimport %environment% %collection%" Set count=0 For %%j in (%processChain1%) Do Set /A count+=1 echo.Total count: %count% pause 

正如你所看到的,我改变了var processChain1结构来分隔空间值(默认的分隔符),并把每个变量放在引号中…至少它的工作原理,并给你总数。

当然只有这样才能使用它。

希望能帮助到你。 干杯。

如果不是,请看看这里,也许是帮助: 在批处理文件中分离令牌

祝你好运


编辑2 (匹配新信息)

批处理文件: Metalhead89.bat

 @echo off :: define the vars set collection=collection1 set environment=oracleDev :: concatenate the vars with ++ set processChain1=-help -startimport++%environment%++%collection% :: Get the total count plus, run each token found Set count=0 For %%j in (%processChain1%) do ( Set /A count+=1 set line=%%j call :processToken ) :: This will be printed out, at the end of the loop echo Total token count: %count% goto :eof :processToken for /f %%f in ("%line%") do ( :: set the command var with your exe file for each token found set command=Running command: java -jar app.jar %%f call :runTheCommand ) goto :eof :runTheCommand :: now we replace the doble ++ in the var string with space, to treat them as options set str=%command% set str=%str:++= % :: finally we do a echo of the command with the option included echo %str% goto :eof 

现在,从命令行调用该文件,你会得到:

 Z:\>call Metalhead89.bat Running command: java -jar app.jar -help Running command: java -jar app.jar -startimport oracleDev collection1 Total token count: 2 

祝你好运哥们;-)

在这里,我要提出我的解决方案,但gmo的解决方案至少和我一样好。

 @echo off rem !!!!!!!!!!!!!!!!!!!!! rem Set Parameter options rem !!!!!!!!!!!!!!!!!!!!! set collection=collection1 set environment=oracleDev rem !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! rem Set modulees As Parameters rem Watch out: Each module + his options has to be in quotation marks rem Options are separated by comma without whitespaces rem !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! set helpmodule="-help" set importmodule="-startimport,%environment%,%collection%" rem !!!!!!!!!!!!!!!!!!!!!!! rem Configure Process Chain rem !!!!!!!!!!!!!!!!!!!!!!! set activeProcessChain=%helpmodulee%,%importmodule% rem !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! rem Start Content Integration Testing Framework rem !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Set count=0 For %%j in (%activeProcessChain%) Do Set /A count+=1 FOR /L %%H IN (1,1,%COUNT%) DO ( call :loopThroughParams %%H ) exit /b :loopThroughParams FOR /F "tokens=%1 delims=," %%I IN ("%activeProcessChain%") Do ( echo. echo. java -jar %nameOfApplication% %%~I ) exit /b :end