2

在我的批处理文件中,我有以下变量:

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 %count%

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

在计算字符串后,我想使用每个参数(来自变量 processChain1 的字符串)启动一个应用程序,我尝试使用:

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

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

4

2 回答 2

1

据我所知,现在计数为 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结构以用空格(默认分隔符)分隔值并将每个 var 放在引号中......至少它有效,并为您提供总数。

当然,如果你能以这种方式使用它。

希望能帮助到你。干杯。

如果没有..看看这里,也许有帮助:批处理文件中的单独标记

祝你好运


EDITED 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

祝你好运哥们;-)

于 2012-08-22T08:38:15.923 回答
0

在这里,我将介绍我的解决方案,但 gmo 的解决方案至少和我的一样好。

@echo off

rem !!!!!!!!!!!!!!!!!!!!!
rem Set Parameter options
rem !!!!!!!!!!!!!!!!!!!!!

set collection=collection1
set environment=oracleDev

rem !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
rem Set Modules 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 helpModul="-help"
set importModul="-startimport,%environment%,%collection%"

rem !!!!!!!!!!!!!!!!!!!!!!!
rem Configure Process Chain
rem !!!!!!!!!!!!!!!!!!!!!!!

set activeProcessChain=%helpModule%,%importModul%

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
于 2012-08-23T08:42:57.753 回答