VBS - 定义数组

VBS - define Array

我写了VBS脚本统计C:\下的所有文件夹,代码如下:

set wshell = createobject("WScript.Shell")
dim fso,file,subfolder,folder
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.CreateTextFile("\192.168.0.201\thang\Learning\test.txt")
Set folder = fso.GetFolder("C:\")
dim i,j
i = 0
j = 0 
For Each subfolder In folder.SubFolders
     'file.WriteLine """" & subfolder.path & """"  'print quotation marks trong VBS
     'arr(i) = subfolder.path
     i=i+1
Next
msgbox "i = " & i   'In my case , C folders has 19 subfolders in there
dim arr
arr = Array(i)  'declare the array which has i member 
' For Each subfolder In folder.SubFolders
     ' 'file.WriteLine """" & subfolder.path & """"  'print quotation marks trong VBS
     ' arr(j) = subfolder.path
     ' j=j+1
' Next

' msgbox arr(0)
' msgbox arr(1) 
msgbox "lbound = " & lbound(arr)  'when ran the code, it always show lbound = 0 
msgbox "ubound = " & ubound(arr)  'when ran the code, it always show ubound = 0 
file.close()

它显示 i = 19 的值,然后我用 i 成员定义了 1 个数组,然后检查它的 lbound 和 ubound,但是它显示 lbound = 0 和 ubound = 0。你能帮我改正我的代码吗?

参见:Array Function

arr = Array(i) 

创建一个包含单个元素 i 的数组。

如果您需要创建一个指定变量大小的数组,您需要使用ReDim Statement

Redim arr(i)