Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP General > Subscript out of range error

Reply
Thread Tools

Subscript out of range error

 
 
Lukelrc
Guest
Posts: n/a
 
      05-19-2004
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>
 
Reply With Quote
 
 
 
 
Aaron Bertrand - MVP
Guest
Posts: n/a
 
      05-19-2004
> Set vFiles =colFolderName.Files
' add this line:
redim ListOfFiles(vFiles.Count-1)


--
Aaron Bertrand
SQL Server MVP
http://www.aspfaq.com/




"Lukelrc" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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>



 
Reply With Quote
 
 
 
 
Mark Schupp
Guest
Posts: n/a
 
      05-19-2004
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.

--
Mark Schupp
Head of Development
Integrity eLearning
www.ielearning.com


"Lukelrc" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting "vector subscript out of range" error Han C++ 4 11-14-2007 07:13 AM
vector subscript out of range Andy C++ 6 05-11-2007 10:05 AM
Runtime error 9: Subscript out of range Excel 2000 jack.jackson11@gmail.com Computer Support 0 01-20-2006 08:47 PM
out of range array subscript Richard Delorme C Programming 5 05-15-2004 03:42 PM
Run-Time error '9' Subscript out of range normanstrong Computer Support 1 08-12-2003 06:22 PM



Advertisments