Dav*_*vid 39 windows command-line for-loop
我正在尝试为我的Windows机器创建一个批处理脚本,它循环遍历一个(字符串/十进制)值列表,并使用每个值作为循环内的参数.
下面是一个简单的for循环的例子,我想用它来显示所有不同的版本文件(从我的列表中)
FOR ? in ('1.1','1.2','2.4','3.9') do echo V[value_from_for_loop].txt
我在如何遍历每个项目并在echo语句中使用变量时遇到问题.
Joe*_*oey 56
for %x in (1.1 1.2 2.4 3.9) do echo V%x.txt
要在批处理文件中使用,您必须加倍%:
for %%x in (1.1 1.2 2.4 3.9) do echo V%%x.txt
小智 24
@ЈЈеу的答案很有用,
这是我如何使用它,以特定的顺序"遍历"预先设置的文件列表.
@echo off
for %%x in (
        a.js
        storage.js
        logic.js
        main.js
        z.js
       ) do (
         echo your file name is %%x
         echo "%%x" is a cool name
         echo.
         echo =-=-=-=-=-=
         echo.
       )
它看起来像垂直列表的原因是更容易添加或删除更多项目.(''''的'echo'是一个空行).
输出将如下所示:
C:\example>yourBatchName.cmd
your file name is a.js
"a.js" is a cool name
=-=-=-=-=-=
your file name is storage.js
"storage.js" is a cool name
=-=-=-=-=-=
your file name is logic.js
"logic.js" is a cool name
=-=-=-=-=-=
your file name is main.js
"main.js" is a cool name
=-=-=-=-=-=
your file name is z.js
"z.js" is a cool name
=-=-=-=-=-=
**ps文件名列表应该更喜欢使用这样的东西:
for %%e in (*.dll) do (.... 
Kur*_*fle 14
假设您有一个很长的值列表,在命令行上输入会非常不舒服.此外,DOS命令行有一个长度限制.
在这种情况下,值可以存储在任意长的文件中,每行一个.调用它my-values.list,内容类似于:
1.1
1.2
2.4
3.9
3.9.1
3.9.2
3.91
3.91.1
...
现在,您可以逐行读取此文本文件中的变量:
for /f "tokens=*" %a in (c:\path\to\my-values.list) do echo.  Version%~nxa.txt