如何进行批处理文件循环以将数组的内容保存到文件

时间:2019-07-07 15:06:25

标签: arrays batch-file

我需要一段Windows批处理代码,该代码可打印从数组的索引0开始到文件的任意长度的内容,每个值都将移至文件中的新行。

这是我尝试使用的代码:

setlocal EnableDelayedExpansion
set i=0
:saveloop
if not defined !array%i%! goto endsaveloop
echo !array%i%! >> examplefile.txt
set/a i+=1
goto saveloop
:endsaveloop

因此,我希望它创建一个名为examplefile.txt的文件,该文件由数组!array%i%!的内容组成,例如,如果数组的内容为 exampledata1exampledata2,  该文件应包含

exampledata1
exampledata2

但是在执行过程中,它只是第一次跳转到循环 if not defined !array%i%! goto endsaveloop 即使!array%i%!当时已明确定义。

2 个答案:

答案 0 :(得分:1)

您可能会找到

set array>examplefile

for /f "tokens=1*delims==" %%a in ('set array 2^>nul') >>examplefile

有用。

set array显示所有以array开头的变量名的 current (即运行时)值。

第一个格式应为

array1=37
array10=61
array11=89
array2=55
...

因此您可以轻松地完全重装阵列

for /f "delims=" %%a in ('type examplefile') do set "%%a"

第二个应该屈服

37
61
89
55

请注意,这严格按照变量名的字母顺序。

对于您的问题,您需要if not defined array%i%,因为!array%i%!是变量的内容,而没有!就是变量本身。例如,如果变量名if (not) defined 4的内容为array%i%,则您将执行4

答案 1 :(得分:0)

如果顺序很重要,我通常使用START i am just and example of a multiline paragraph having some unique informations in each para. I end with a capital END. START i am just and example of a multiline paragraph having some unique informations in each para. You see in this paragraph I have one extra line.I end with a capital END. 遍历数组,只要我填充0即可。

相关问题