Subscript out of range error

Discussion in 'ASP General' started by Lukelrc, May 19, 2004.

  1. Lukelrc

    Lukelrc Guest

    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>
     
    Lukelrc, May 19, 2004
    #1
    1. Advertising

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


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




    "Lukelrc" <> wrote in message
    news:...
    > 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>
     
    Aaron Bertrand - MVP, May 19, 2004
    #2
    1. Advertising

  3. Lukelrc

    Mark Schupp Guest

    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" <> wrote in message
    news:...
    > 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>
     
    Mark Schupp, May 19, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Richard Delorme

    out of range array subscript

    Richard Delorme, May 3, 2004, in forum: C Programming
    Replies:
    5
    Views:
    482
    Chris Torek
    May 15, 2004
  2. Andy
    Replies:
    6
    Views:
    712
    James Kanze
    May 11, 2007
  3. Han
    Replies:
    4
    Views:
    8,022
  4. yogi_bear_79
    Replies:
    11
    Views:
    971
    James Kanze
    Mar 16, 2008
  5. Replies:
    18
    Views:
    227
    McKirahan
    Jan 12, 2005
Loading...

Share This Page