从 Windows 命令行 bat 文件循环遍历字符串值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3439954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:31:10  来源:igfitidea点击:

Looping through string values from a windows command line bat file

windowsfor-loopcommand-line

提问by David

I am trying to create a batch script for my Windows machine that loops through a list of (string/decimal) values and uses each value as a parameter inside the loop.

我正在尝试为我的 Windows 机器创建一个批处理脚本,该脚本循环遍历(字符串/十进制)值列表并将每个值用作循环内的参数。

Below is an example of a simple for loop I would like to use to display all the different version files (from my list)

下面是一个简单的 for 循环示例,我想用它来显示所有不同的版本文件(来自我的列表)

FOR ? in ('1.1','1.2','2.4','3.9') do echo V[value_from_for_loop].txt

I am having trouble in how to loop through each item and use a variable in my echo statement.

我在如何遍历每个项目并在我的 echo 语句中使用变量时遇到了麻烦。

回答by Joey

for %x in (1.1 1.2 2.4 3.9) do echo V%x.txt

For use in a batch file you'll have to double the %:

要在批处理文件中使用,您必须将以下内容加倍%

for %%x in (1.1 1.2 2.4 3.9) do echo V%%x.txt

回答by David

@?οеу's answer works great,

@?οе他们的回答很好用,

here is how I've used it, to 'walk' a pre-set list of files in a specific order.

这是我如何使用它,以特定顺序“遍历”预设的文件列表。

@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.
       )

the reason it looks like a vertical list is so it will be easier to add or remove more items. (and 'echo' with 'dot' is for one empty line).

它看起来像一个垂直列表的原因是,添加或删除更多项目会更容易。(“回声”与“点”是一个空行)。

the output will look like this:

输出将如下所示:

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

=-=-=-=-=-=

** p.s. for file name listing one should prefer using something like this:

** 用于文件名列表的 ps 应该更喜欢使用这样的东西:

for %%e in (*.dll) do (....

for %%e in (*.dll) do (....

回答by Kurt Pfeifle

Assume you have a very long list of values which will be very uncomfortable to type on the commandline. Also, there is a length limit for the DOS command line.

假设你有一个很长的值列表,在命令行上输入会很不舒服。此外,DOS 命令行也有长度限制。

In this case the values may be stored in an arbitrarily long file, one per line. Call it my-values.list, with a content similar to:

在这种情况下,值可以存储在任意长的文件中,每行一个。调用它my-values.list,内容类似于:

1.1
1.2
2.4
3.9
3.9.1
3.9.2
3.91
3.91.1
...

Now you could read the variables from this text file, line by line:

现在您可以从该文本文件中逐行读取变量:

for /f "tokens=*" %a in (c:\path\to\my-values.list) do echo.  Version%~nxa.txt

回答by Ted O'Connor

It won't let me comment, but I wanted to add my 2 cents worth here. The ' do ' has to be on the same line as the right parenthesis of the 'for' command. In other words, this will work:

它不会让我发表评论,但我想在这里添加我的 2 美分。“do”必须与“for”命令的右括号在同一行。换句话说,这将起作用:

for %x in (1.1 1.2 2.4 3.9) do echo V%x.txt

...but this will not:

...但这不会:

for %x in (1.1 1.2 2.4 3.9)
    do echo V%x.txt

回答by mwpowellhtx

Something like this, I believe:

像这样的事情,我相信:

for %x in (1.1 1.2 2.4 3.9) do (
    echo V%x.txt
)