在 Windows Batch 中创建列表或数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17605767/
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 10:42:09  来源:igfitidea点击:

Create list or arrays in Windows Batch

windowsbatch-file

提问by user2533789

Can I declare a list or array in a batch file like this:

我可以在批处理文件中声明一个列表或数组,如下所示:

set list = "A B C D"

And then I need to write these to a file, with the spaces between:

然后我需要将这些写入文件,中间有空格:

A

B

C 

D

回答by Aacini

Yes, you may use both ways. If you just want to separate the elements and show they in separated lines, a list is simpler:

是的,您可以同时使用两种方式。如果您只想分隔元素并将它们显示在单独的行中,则列表更简单:

set list=A B C D

A list of values separated by space may be easily processed by forcommand:

可以通过for命令轻松处理由空格分隔的值列表:

(for %%a in (%list%) do (
   echo %%a
   echo/
)) > theFile.txt

You may also create an array this way:

你也可以这样创建一个数组:

setlocal EnableDelayedExpansion
set n=0
for %%a in (A B C D) do (
   set vector[!n!]=%%a
   set /A n+=1
)

and show the array elements this way:

并以这种方式显示数组元素:

(for /L %%i in (0,1,3) do (
   echo !vector[%%i]!
   echo/
)) > theFile.txt

For further details about array management in Batch files, see: Arrays, linked lists and other data structures in cmd.exe (batch) script

有关批处理文件中数组管理的更多详细信息,请参阅:cmd.exe(批处理)脚本中的数组、链表和其他数据结构

ATTENTION! You must know that all characters included in setcommand are inserted in the variable name (at left of equal sign), or in the variable value. For example, this command:

注意力!您必须知道setcommand 中包含的所有字符都插入到变量名(等号左侧)或变量值中。例如,这个命令:

set list = "A B C D"

create a variable called list(list-space) with the value "A B C D"(space, quote, A, etc). For this reason, it is a good idea to never insert spaces in setcommands. If you need to enclose the value in quotes, you must enclose both the variable name and its value:

list使用值"A B C D"(空格、引号、A 等)创建一个名为(list-space)的变量。因此,最好不要在set命令中插入空格。如果需要将值括在引号中,则必须将变量名及其值括起来:

set "list=A B C D"

PS - You should NOT use ECHO.in order to left blank lines! An alternative is ECHO/. For further details about this point, see: http://www.dostips.com/forum/viewtopic.php?f=3&t=774

PS - 你不应该ECHO.为了留空行而使用!另一种选择是ECHO/。有关这一点的更多详细信息,请参阅:http: //www.dostips.com/forum/viewtopic.php?f=3&t=774

回答by John Zhu

Sometimes the array element may be very long, at that time you can create an array in this way:

有时数组元素可能很长,这时候可以这样创建数组:

set list=a
set list=%list%;b 
set list=%list%;c 
set list=%list%;d

Then show it:

然后显示它:

@echo off
for %%a in (%list%) do ( 
 echo %%a
 echo/
)

回答by Magoo

@echo off
setlocal
set "list=a b c d"
(
 for %%i in (%list%) do (
  echo(%%i
  echo(
 )
)>file.txt

You don't need - actually, can't "declare" variables in batch. Assigning a value to a variable creates it, and assigning an empty string deletes it. Any variable name that doesn't have an assigned value HAS a value of an empty string. ALL variables are strings - WITHOUT exception. There ARE operations that appear to perform (integer) mathematical functions, but they operate by converting back and forth from strings.

您不需要 - 实际上,不能批量“声明”变量。给一个变量赋值会创建它,而给一个空字符串赋值会删除它。任何没有赋值的变量名都有一个空字符串的值。所有变量都是字符串 - 没有异常。有一些操作似乎执行(整数)数学函数,但它们通过从字符串来回转换来进行操作。

Batch is sensitive to spaces in variable names, so your assignment as posted would assign the string "A B C D"- including the quotes, to the variable "list "- NOT including the quotes, but including the space. The syntax set "var=string"is used to assign the value stringto varwhereas set var=stringwill do the same thing. Almost. In the first case, any stray trailing spaces after the closing quote are EXCLUDED from the value assigned, in the second, they are INCLUDED. Spaces are a little hard to see when printed.

Batch 对变量名称中的空格很敏感,因此您发布的赋值会将字符串"A B C D"(包括引号)分配给变量"list "- 不包括引号,但包括空格。语法set "var=string"用于将值分配stringvarset var=string将做同样的事情。几乎。在第一种情况下,结束引号后的任何杂散尾随空格都从分配的值中排除,在第二种情况下,它们被包括在内。打印时有点难以看到空格。

ECHOechoes strings. Clasically, it is followed by a space - one of the default separators used by batch (the others are TAB, COMMA, SEMICOLON - any of these do just as well BUT TABS often get transformed to a space-squence by text-editors and the others have grown quirks of their own over the years.) Other characters following the Oin ECHOhave been found to do precisely what the documented SPACE should do. DOT is common. Open-parenthesis (is probably the most useful since the command

ECHO回声字符串。通常,它后跟一个空格 - 批处理使用的默认分隔符之一(其他是 TAB、COMMA、SEMICOLON - 这些中的任何一个都一样好,但 TABS 经常被文本编辑器转换为空格序列,并且多年来,其他人已经养成了自己的怪癖。)已经发现Oin之后的其他字符ECHO正是记录的 SPACE 应该做的。DOT 很常见。开括号(可能是自命令以来最有用的

ECHO.%emptyvalue%

will produce a report of the ECHOstate (ECHO is on/off) whereas

将产生ECHO状态报告( ECHO is on/off) 而

ECHO(%emptyvalue%

will produce an empty line.

将产生一个空行。

The problem with ECHO(is that the result "looks" unbalanced.

问题ECHO(在于结果“看起来”不平衡。

回答by Schickmeister

Array type does not exist

数组类型不存在

There is no 'array' type in batch files, which is both an upside and a downside at times, but there are workarounds.

批处理文件中没有“数组”类型,这有时有利也有弊,但有解决方法。

Here's a link that offers a few suggestions for creating a system for yourself similar to an array in a batch: http://hypftier.de/en/batch-tricks-arrays.

这是一个链接,提供了一些为自己创建类似于批处理数组的系统的建议:http: //hypftier.de/en/batch-tricks-arrays

  • As for echoing to a file echo variable >> filepathworks for echoing the contents of a variable to a file,
  • and echo.(the period is not a typo) works for echoing a newline character.
  • 至于回显到文件echo variable >> filepath用于将变量的内容回显到文件中,
  • 并且echo.(句点不是错字)适用于回显换行符。

I think that these two together should work to accomplish what you need.

我认为这两者应该一起工作来完成你所需要的。

Further reading

进一步阅读

回答by tux

@echo off

set array=

setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION

set nl=^&echo(

set array=auto blue ^!nl!^
  bycicle green ^!nl!^
  buggy   red

echo convert the String in indexed arrays

set /a index=0

for /F "tokens=1,2,3*" %%a in ( 'echo(!array!' ) do (

 echo(vehicle[!index!]=%%a color[!index!]=%%b 
 set vehicle[!index!]=%%a
 set color[!index!]=%%b
 set /a index=!index!+1   

)

echo use the arrays

echo(%vehicle[1]% %color[1]%
echo oder

set index=1
echo(!vehicle[%index%]! !color[%index%]!