VBS - define Array

Viewed 79

I wrote the VBS script to count all the folders under C:\ , the code as below:

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()

It show the value of i = 19 , then i define 1 array with i members , then check its lbound and ubound , however it shows lbound = 0 and ubound = 0. Can you please help correct my code ?

enter image description here

1 Answers

See: Array Function

arr = Array(i) 

creates an array with a single element i.

If you need to create an array specifying a variable as size, you need to use the ReDim Statement

Redim arr(i)
Related