批处理指令中并没有原生支持数组,但这不代表不能模拟出来。

来看下面的代码:

1
2
3
4
5
6
7
SET Obj_Length=2

SET Obj[0].Name=Test1
SET Obj[0].Value=Hello World

SET Obj[1].Name=Test2
SET Obj[1].Value=blahblah

这样就定义了一个数组了,而且是结构体的数组。

在需要取值的时候,可以这么玩:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
SET Obj_Index=0

:LoopStart
IF %Obj_Index% EQU %Obj_Length% GOTO :EOF

SET Obj_Current.Name=0
SET Obj_Current.Value=0

FOR /F "usebackq delims==. tokens=1-3" %%I IN (`SET Obj[%Obj_Index%]`) DO (
    SET Obj_Current.%%J=%%K
)

ECHO Name = %Obj_Current.Name%
ECHO Value = %Obj_Current.Value%
ECHO.

SET /A Obj_Index=%Obj_Index% + 1

GOTO LoopStart

输出结果:

Name = Test1
Value = Hello World

Name = Test2
Value = blahblah

怎么样,很不错吧~~

您还可能感兴趣的日志:

  1. 强大的批处理 (2) Fibonacci函数
  2. 强大的批处理 (1) 字符串截取
  3. 强大的批处理 (3) 判断目录在%Path%中

  2 Responses to “强大的批处理 (4) 使用数组”

  1. 顶你…..
    正需要…. 谢了哈

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

   
© 2004 - 2011 Leona+Suffusion theme by Sayontan Sinha