遍历数组批处理脚本

Adr*_*der 0 windows cmd shell32 batch-file

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

在此处输入图片说明

@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]! 
)
Run Code Online (Sandbox Code Playgroud)

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

在此处输入图片说明

Nah*_*eul 5

在批处理空间中很重要,因为它是一个参数定界符

set topic[0] = comments
Run Code Online (Sandbox Code Playgroud)

应该

set topic[0]=comments
Run Code Online (Sandbox Code Playgroud)

cmd的奇怪之处在于变量可以以空格结尾

set topic[0] = comments
echo %topic[0] %
 comments
Run Code Online (Sandbox Code Playgroud)