迭代数组批处理脚本

时间:2017-06-05 14:36:35

标签: windows batch-file cmd shell32

在遵循批处理脚本中迭代数组的教程时,我没有得到相同的结果:

enter image description here

@echo off 
setlocal enabledelayedexpansion 
set topic[0] = comments 
set topic[1] = variables 
set topic[2] = Arrays 
set topic[3] = Decision making 
set topic[4] = Time and date 
set topic[5] = Operators 

for /l %%n in (0,1,5) do ( 
   echo !topic[%%n]! 
)

当我运行此命令时,我得到:

enter image description here

1 个答案:

答案 0 :(得分:4)

批处理空间中的

很重要,因为它是一个参数分隔符

set topic[0] = comments

应该是

set topic[0]=comments

cmd奇怪的是变量可以用空格终止

set topic[0] = comments
echo %topic[0] %
 comments