如何在批处理脚本中使用数组索引?

时间:2022-09-28 02:07:04

i want set varable instead index of array in loop (batch script) like:

我想要设置varable而不是循环中的数组索引(批处理脚本),如:

@ECHO OFF

set array[1]=22750289 512
set array[2]=22750289 5600
set array[3]=22750289 5612

for %%N in (1,1,3) do (
    echo %array[%%i]%
    echo %array[1]%
 )

but result : ECHO is off. 22750289 512 ECHO is off. 22750289 512 ECHO is off. 22750289 512

但结果是:ECHO关闭了。 22750289 512 ECHO关闭。 22750289 512 ECHO关闭。 22750289 512

1 个解决方案

#1


2  

For this you need to enable delayed variable expansion using setlocal:

为此,您需要使用setlocal启用延迟变量扩展:

@echo off

setlocal EnableDelayedExpansion

set array[1]=22750289 512
set array[2]=22750289 5600
set array[3]=22750289 5612

for /L %%N in (1,1,3) do (
echo !array[%%N]!)

endlocal

Notice that the variables are no longer available after endlocal.

请注意,endlocal之后变量不再可用。

For more information about delayed variable expansion reference this thread.

有关延迟变量扩展的更多信息,请参考此线程。

#1


2  

For this you need to enable delayed variable expansion using setlocal:

为此,您需要使用setlocal启用延迟变量扩展:

@echo off

setlocal EnableDelayedExpansion

set array[1]=22750289 512
set array[2]=22750289 5600
set array[3]=22750289 5612

for /L %%N in (1,1,3) do (
echo !array[%%N]!)

endlocal

Notice that the variables are no longer available after endlocal.

请注意,endlocal之后变量不再可用。

For more information about delayed variable expansion reference this thread.

有关延迟变量扩展的更多信息,请参考此线程。