0

I have a list that is saved in arraylist.txt file

-blank-
A1
A2
A3
A4

then i need to read the text file, split it and have an array consist if the value of text file.

 Const ForReading = 1
Dim arrServiceList
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("D:\TestStatus\arraylist.txt", ForReading)

Do Until objTextFile.AtEndOfStream
  strNextLine = objTextFile.Readline
  arrServiceList = Split(strNextLine , "")
  Wscript.Echo "Server name: " & arrServiceList(0)
  For i = 1 To UBound(arrServiceList)
    WScript.Echo "Service: " & arrServiceList(i)
  Next
Loop

This is what I do to read and split the content of file. But when I try:

msgbox arrServiceList(0)

It prompt A4. Is it supposed to be -blank-? Then when I try to:

msgbox arrServiceList(1)

Error:

Error: subscript out of range.

Please help.

3
  • as your code you must see that error...you have one for for read from file...When read one line THEN try to access arrServiceList...arrServiceList have no item or only 1 item and you try to access arrServiceList(1) then see that error Commented Jun 16, 2017 at 5:29
  • try to write 2 separate for...1 for read from file and one for loop on arrServiceList Commented Jun 16, 2017 at 5:30
  • In your split statement, you have used an empty string as a delimiter(""). Why? May be you should post the exact contents of your text file so that we can help you. Commented Jun 16, 2017 at 5:40

1 Answer 1

0

In your other question your input file had no blank first line. So fire the idiot who put it there.

@JosefZ told you that VBScript arrays count from Zero to UBound()/Last Index. So read answers carefully.

>> s = "splitting a string on the empty string """" gives an one elm array containg that string"
>> a = Split(s, "")
>> WScript.Echo LBound(a), UBound(a), a(0)
>>
0 0 splitting a string on the empty string "" gives an one elm array containg that string

So after the loop, your array of size 1/UBound 0 contained the last line of your file. Trying to access the second element/index 1 caused the error.

To read a one-column file into an array, use ReadAll() and Split() on the EOL delimiter. As in:

Option Explicit

Dim s : s = CreateObject("Scripting.FileSystemObject").OpenTextFile(".\44581406.txt").ReadAll()
WScript.Echo s
WScript.Echo "------------"
Dim a : a = Split(s, vbCrLf)
WScript.Echo Join(a, "<>")

output:

cscript 44581406.vbs
A1
A2
A3

Look at the output carefully - do you see the problem/draw-back of the SplitOnReadAll approach?

Use the docs to make sure that you understand the concepts, objects, and methods.

Sign up to request clarification or add additional context in comments.

1 Comment

Mystery down voter, have some decency to justify your action.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.