瓦尔伯爵在Windows批 [英] Count Var in Windows Batch

查看:152
本文介绍了瓦尔伯爵在Windows批的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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%

但是,在第一个错误。它打印出1和2,不为什么?

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

计数的琴弦后,我要开始与每个参数(自变量processChain1字符串)的应用程序
我尝试它:

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,因为有在VAR只有一根弦,你正在分裂后,但你的令牌计数已被设置为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....

您需要每个结果拆分第一个字符串(delims =,),然后在第二部分工作。

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

EDITED

试试这个...

@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 的结构与空间(默认分隔符)分隔值,并把每一个变种的报价...
至少它的工作原理,并为您的总数。

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.

如果不是..看看这里,也许它的帮助:在批处理文件<分隔标记/ A>

If not.. take a look here, maybe it's help : separate tokens in 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 ;-)

这篇关于瓦尔伯爵在Windows批的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆