简体   繁体   English

循环通过Windows命令行bat文件中的字符串值

[英]Looping through string values from a windows command line bat file

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语句中使用变量时遇到问题。

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

@Јοеу'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). (''''的'echo'是一个空行)。

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

=-=-=-=-=-=

** ps for file name listing one should prefer using something like this: ** ps文件名列表应该更喜欢使用这样的东西:

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

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.listmy-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

It won't let me comment, but I wanted to add my 2 cents worth here. 它不会让我评论,但我想在这里加上我的2美分。 The ' do ' has to be on the same line as the right parenthesis of the 'for' command. 'do'必须与'for'命令的右括号位于同一行。 In other words, this will work: 换句话说,这将有效:

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

Something like this, I believe: 我相信这样的事情:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从Windows命令行执行外部PHP文件(bat文件) - How to execute an external PHP file from a Windows Command Line (bat file) Windows:使用(非默认)shell动词(如.bat或命令行中的“edit”)启动文件 - Windows: start a file using a (non-default) shell verb like “edit” from .bat or command-line 如何用命令行参数字符串替换bat文件中的字符串 - How to replace string inside a bat file with command line parameter string 如何使用bat文件响应命令行提示符(Windows) - How to respond to command line prompt using bat file (Windows) 循环遍历目录,使用bat文件上传目录 - looping through a directory in bat, going up a directory using bat file 使用命令行参数从bat文件运行exe - Run exe from bat file with command line arguments 如何:从Windows命令行搜索文件中的字符串? - Howto: Searching for a string in a file from the Windows command line? 从命令窗口执行并双击bat文件时,Windows .Bat文件的行为有所不同 - Windows .Bat file behave differently when executed from command window and by double clicking on the bat file Windows bat 脚本因多行命令而失败 - Windows bat script fails with command on multiple line 对于Windows,是一个命令行bat文件,显示目录中每个文件的总行数 - For windows, a command-line bat file that displays total lines in each file in a directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM