8

I am trying to read a file into an array list and then return it from a function. My function works to read the file to an array but when I try setting it to a variable it errors saying invalid procedure call or argument

My read file code

function readfile(strFile)
dim fs,objTextFile 
set fs=CreateObject("Scripting.FileSystemObject")
dim arrStr
set objTextFile = fs.OpenTextFile(strFile)
Set userArrayList = CreateObject( "System.Collections.ArrayList" )

Do Until objTextFile.AtEndOfStream 
strNextLine = objTextFile.Readline 
userArrayList.add strNextLine
Loop 

objTextFile.Close
set objTextFile = Nothing
set fs = Nothing

readfile = userArrayList
end function

Calling it in my code

arr = readfile("\\dc1\NETLOGON\Scripts\Add_Users\user.csv")

For Each present In arr
user = split(present,",")
WScript.Echo user(0) & user(1) & user(2) & user(3) & user(4) & "|"
Next

What am I doing wrong?

1 Answer 1

11

Turns out I need to use set here is the working code. Working function

function readfile(strFile)
dim fs,objTextFile 
set fs=CreateObject("Scripting.FileSystemObject")
dim arrStr
set objTextFile = fs.OpenTextFile(strFile)
Set userArrayList = CreateObject( "System.Collections.ArrayList" )

Do Until objTextFile.AtEndOfStream 
strNextLine = objTextFile.Readline 
userArrayList.add strNextLine
Loop 

objTextFile.Close
set objTextFile = Nothing
set fs = Nothing

set readfile = userArrayList
end function

Working Call

Dim arr
set arr = readfile("\\dc1\NETLOGON\Scripts\Add_Users\user.csv")
For Each present In arr
user = split(present,",")
WScript.Echo user(0) & user(1) & user(2) & user(3) & user(4) & "|"
'WScript.Echo present & "|"
Next
Sign up to request clarification or add additional context in comments.

2 Comments

Sometimes it could be just that simple :). +1 for finding and posting your solution.
Nice, life saver!

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.