Count Var in Windows Batch

一曲冷凌霜 提交于 2020-01-24 15:28:26

问题


In my batch file I have the following variables:

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

As you can see, my process chain contains two strings that are separated with a ",".

Now I want to count the two strings (later it could be more then one string). I tried it with:

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

But there is the first mistake. It prints out 1 and not 2. Why?

After counting the strings I want to start an application with each parameter (string from the variable processChain1) I try it with:

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

This cant work correct now because the counter is wrong because of the first mistake. But I think if I can solve the first problem, the second should work fine. Is this correct?


回答1:


As far I can tell, right now, is counting 1 because there's only one string in that var, you are making the split later, but your token count is already set to 1....

You need to split the first string (delims=,) and then in the second part, work with each result.

EDITED:

Try this...

@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

As you can see, I change the var processChain1 structure to separate the values with a space (default delimeter) and put every var in quotes... At least it works, and gives you the total count.

Only of course, If you can use it in this way.

Hope it helps. Cheers.

If not.. take a look here, maybe it's help : separate tokens in batch file

Good luck


EDITED 2 (to match new information)

Batch file: 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

Now, Call that file from command line and you will get:

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

Good luck buddy ;-)




回答2:


Here I am going to present my solution but the solution of gmo is at least as good as mine.

@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


来源:https://stackoverflow.com/questions/12068698/count-var-in-windows-batch

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