Windows Batch循环虽然带有动态令牌计数的变量

前端之家收集整理的这篇文章主要介绍了Windows Batch循环虽然带有动态令牌计数的变量前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在我的 Windows批处理文件中,我有一些带有不同数量字符串的变量.
为了exapmle:
  1. set string="-start" "-end someOption"

我用以下方式计算String的数量

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

输出将是:

  1. Total count: 2

现在我想在我的变量中使用Strings多次启动一个应用程序,我想给应用程序提供当前字符串作为参数.我试过这个:

  1. FOR /L %%H IN (1,1,%COUNT%) DO (
  2.  
  3. echo %%H
  4. FOR /F "tokens=%%H " %%I IN ("%string%") Do (
  5. echo %%I
  6. rem java -jar app.jar %%I
  7. )
  8. )

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

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.

如何在变量“string”中循环遍历两个字符串?

您不能在FOR / F的选项字段中使用FOR参数或延迟扩展变量.
但是你可以创建一个函数并使用百分比扩展.

拆分是delim字符的效果,它是默认空间和制表符,它们也适用于带引号的参数.
所以我将分隔符更改为分号,然后就可以了.

  1. set string="-start";"-end someOption"
  2. set count=0
  3. For %%j in (%string%) Do Set /A count+=1
  4. echo.Total count: %count%
  5.  
  6. FOR /L %%H IN (1,%COUNT%) DO (
  7.  
  8. echo %%H
  9. call :myFunc %%H
  10. )
  11. exit /b
  12. :myFunc
  13. FOR /F "tokens=%1 delims=;" %%I IN ("%string%") Do (
  14. echo %%~I
  15. rem java -jar app.jar %%I
  16. )
  17. exit /b

猜你在找的Windows相关文章