在Windows中创建批处理列表或数组 [英] Create list or arrays in Windows Batch
问题描述
我可以声明在这样的批处理文件列表或数组:
集列表=A B C D
然后我需要它们写入一个文件,与之间的空间:
A乙Cð
是的,你可以用两种方式。如果你只是想的元素分离,并显示他们分开的线,列表是简单的:
设置列表= A B C D
用空格分隔值的列表,可以很容易地的处理
命令:
(对于一个%%在(%列表%)做(
回声%%一
回声/
))GT; theFile.txt
您也可以创建一个数组是这样的:
SETLOCAL EnableDelayedExpansion
设定N = 0
对于一个%%在(A B C D)做(
设置向量[!N!= %%一
集/ A N + = 1
)
和展示数组元素是这样的:
(用于/ L %% i的(0,1,3)做(
回音!矢量[%% I]!
回声/
))GT; theFile.txt
有关在批处理文件阵列管理的详细信息,请参见:<一href=\"http://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990\">Arrays,链表和其他数据结构中的cmd.exe(批)脚本
注意!你必须知道,包含在集
命令插入变量名(在等号的左边),或者在变量值的所有字符。例如,下面的命令:
集列表=A B C D
与值创建一个名为列表
(表空间)变量ABCD
(空格,报价, A,等等)。出于这个原因,它是永远不会插入集
命令空间是一个好主意。如果你需要用引号括起来的价值,则必须用两个变量的名称和它的值:
设置列表= A B C D
PS - 你不应该使用 ECHO
为了留下空行!另一种方法是 ECHO /
。有关这一点的详细信息,请参见:<一href=\"http://www.dostips.com/forum/viewtopic.php?f=3&t=774\">http://www.dostips.com/forum/viewtopic.php?f=3&t=774
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
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 for
command:
(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
ATTENTION! You must know that all characters included in set
command are inserted in the variable name (at left of equal sign), or in the variable value. For example, this command:
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 set
commands. If you need to enclose the value in quotes, you must enclose both the variable name and its value:
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
这篇关于在Windows中创建批处理列表或数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!