Subscript out of range error

L

Lukelrc

Hi,

I have some VBscript that lists the files in a folder. What i want to
do is populate an array with the file names and then use that array to
populate a list box. I have got as far as populating the array, but i
can't get rid of the error 'Subscript Out Of Range' Can anyone see
where i have gone wrong?

<script language = vbscript runat=Server>
Dim VFileItem
Dim vFiles
Dim objFileSysOb
Dim colFolderName
Dim Count
Count = 0
Dim ListOfFiles()
vSFolder = Session("availablepath")
Set objFileSysOb = Server.CreateObject("Scripting.FileSystemObject")
Set colFolderName = objFileSysOb.GetFolder(vSFolder)
Set vFiles =colFolderName.Files
Response.Write "<b>The Files in the folder are:</b><p>"
For each vFileItem in vFiles
ListofFiles(Count) = vFileItem
Count = Count + 1
Next
for i = 0 to Count
document.write(ListOfFiles(i) & "<br />")
next
</script>
 
A

Aaron Bertrand - MVP

Set vFiles =colFolderName.Files
' add this line:
redim ListOfFiles(vFiles.Count-1)
 
M

Mark Schupp

You have to set the array bounds of ListOfFiles.

You can either set it statically if you know the largest size you will ever
need as in:
Dim ListOfFiles(100)
or you can redimension it as needed as in:

Dim ListOfFiles()

ReDim ListOfFiles(10)
...
If nCount > UBound(ListOfFiles) Then ReDim Preserve
ListOfFiles(nCount+10)
...

You might want to check the documentation of the Files collection. There may
be a way to get the array of data directly from that. I think you could also
access that collection as if it were an array.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top