Windows批处理循环尽管变量,动态令牌数 [英] Windows Batch loop though variable with dynamic token count

查看:154
本文介绍了Windows批处理循环尽管变量,动态令牌数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Windows批处理文件,我有一个不同的数字串的局部变量。
对于exapmle:

In my Windows batch file I have some variable with a various number of strings. For exapmle:

set string="-start" "-end someOption" 

我算一个字符串的数字方式如下:

I count the numbers of a String the following way:

Set count=0
For %%j in (%string%) Do Set /A count+=1
echo.Total count: %count%

将输出:

Total count: 2

现在我要开始申请多次,我在变量有一个字符串,我想给应用程序的当前字符串作为参数。我想这样的:

Now I want to start an application as many times as I have Strings in my variable and I want to give the application the current string as parameter. I tried this:

 FOR /L %%H IN (1,1,%COUNT%) DO ( 

    echo %%H
        FOR /F "tokens=%%H " %%I IN ("%string%") Do (
            echo %%I
            rem java -jar app.jar %%I
        )
    )

但不幸的是这不起作用:这就是输出:

But unfortunately this does not work: Thats the output:

当前String数:1%H,卡恩syntaktisch的dieser颗星
  nicht verarbeitet werden。 (%H不能syntacticaly在此使用的
  地方)当前String数:2%H,卡恩syntaktisch的dieser
  斯特尔nicht verarbeitet werden。

Number of current String: 1 "%H "" kann syntaktisch an dieser Stelle nicht verarbeitet werden. (%H "" can not be used syntacticaly at this place) Number of current String: 2 "%H "" kann syntaktisch an dieser Stelle nicht verarbeitet werden.

如何通过我的变量字符串两个字符串循环?

How can I loop through the two Strings in my variable "string"?

推荐答案

您不能使用FOR-参数,也不在的FOR / F选项字段延迟扩展变量。结果
但是你可以创建一个函数,并使用有百分号展开。

You can't use a FOR-parameter nor a delayed expanded variable at the option field of FOR/F.
But you can create a function and use there percent expansion.

分裂是delim中字符的效果,这是每默认空间和标签,他们也努力在所列出的参数。结果
所以我改变了你的分隔符分号,那么它的工作原理。

The splitting is an effect of the delim characters, it's per default space and tab, and they work also in the quoted parameter.
So I changed your delimiter to a semicolon, then it works.

set string="-start";"-end someOption" 
set count=0
For %%j in (%string%) Do Set /A count+=1
echo.Total count: %count%

FOR /L %%H IN (1,1,%COUNT%) DO ( 

    echo %%H
    call :myFunc %%H
)
exit /b
:myFunc
FOR /F "tokens=%1 delims=;" %%I IN ("%string%") Do (
  echo %%~I
  rem java -jar app.jar %%I
)
exit /b

这篇关于Windows批处理循环尽管变量,动态令牌数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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