none
Using a variable to set the size of an array

    Question

  • Hey all,

    Okay, so I am writing a script that uses an array to hold file names and then will display them. However, because I do not know how many files will be in the folder, I have to set the size of an array to a variable so I can ReDim Preserve. However, when i first assign the size of an array (using the variable 'i'), I get an error message saying: Expected integer constant, but, I believe everything else is okay, so here is where I need help:

    i = 0
    Dim arrFiles(i)          <------------This is where it is expecting the integer constant
    For Each oFile In colFiles
      arrFiles() = oFile.Name
      WScript.Echo "arrFiles(" & i & ") = " & arrFiles(i)
      i = i + 1
      Redim Preserve arrFiles(i)
    Next

    However, if I put a '0' instead of an 'i' where indicated in the code, it loops through until 'Redim Preserve arrFiles(i)' saying this array is fixed or temporarily locked.

    I just don't understand why I am getting these errors, any help is appreciated!

    Saturday, February 16, 2013 8:28 PM

Answers

  • Yes Dim an array requires a constant and not a variable.

    This will work:

    Set fso=CreateObject("Scripting.fileSystemObject")
    set colFiles = fso.GetFolder("c:\scripts").Files
    
    ReDim arrFiles(colfiles.count)
    i = 0
    For Each oFile In colFiles
       arrFiles(i) = oFile.Name
       wscript.Echo arrFiles(i)
        i = i + 1
    Next

    I believe this is what Oberwald intended and I. too, missed it.

    If you know the count then just size the Redimed array once at the beginning.  It gets set to count and not count -1. count.  Count -1 is the upper bound or maximum zero based index.

    The real lesson here size that you should not be using arrays.  Files is already a portable collection which is enumerable.


    ¯\_(ツ)_/¯

    • Proposed as answer by jrv Saturday, February 16, 2013 11:37 PM
    • Marked as answer by Rover_Dude Sunday, February 17, 2013 12:00 AM
    Saturday, February 16, 2013 11:37 PM

All replies

  • Since you do know the number of files in your collection, you can size up your array right at the start:

    Dim arrFiles(colFiles.count -1)         <------------This is where it is expecting the integer constant
    i = 0
    For Each oFile In colFiles

       arrFiles(i) = oFile.Name
       i = i + 1
    Next



    Saturday, February 16, 2013 8:39 PM
  • Oberwald's answer is the best method but, for the future,  her is how to do it with a re-dimensionable array:

    Dim arrFiles()
    
    i=0
    For Each oFile In colFiles
       arrFiles(i) = oFile.Name
       Redim Preserve arrFiles(i+1)
       WScript.Echo "arrFiles(" & i & ") = " & arrFiles(i)
       i = i + 1
    Next

    The array must be declared with no size and then added to as used.  The number to add is always one more than the index because ii is an absolute size that is used to declare the redim.


    ¯\_(ツ)_/¯

    Saturday, February 16, 2013 10:17 PM
  • Well, I tried your method, but it still did not recognize the colFiles.Count - 1 as an integer. I understand what you were doing, however, it did not work.
    Saturday, February 16, 2013 10:52 PM
  • Hey, this worked!

    well, almost. I just had to do a ReDim before the For loop. Thank you all for the help, I don't know why this confused me so much. I guess I have always had trouble with arrays (in scripting and Visual Basic.)

    Thank you!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    Saturday, February 16, 2013 10:55 PM
  • Yes Dim an array requires a constant and not a variable.

    This will work:

    Set fso=CreateObject("Scripting.fileSystemObject")
    set colFiles = fso.GetFolder("c:\scripts").Files
    
    ReDim arrFiles(colfiles.count)
    i = 0
    For Each oFile In colFiles
       arrFiles(i) = oFile.Name
       wscript.Echo arrFiles(i)
        i = i + 1
    Next

    I believe this is what Oberwald intended and I. too, missed it.

    If you know the count then just size the Redimed array once at the beginning.  It gets set to count and not count -1. count.  Count -1 is the upper bound or maximum zero based index.

    The real lesson here size that you should not be using arrays.  Files is already a portable collection which is enumerable.


    ¯\_(ツ)_/¯

    • Proposed as answer by jrv Saturday, February 16, 2013 11:37 PM
    • Marked as answer by Rover_Dude Sunday, February 17, 2013 12:00 AM
    Saturday, February 16, 2013 11:37 PM