Curd Wong
Curd Wong

Reputation: 1

vbs type mismatch error 800a000d type mismatch : readfile

I am new at vbs and am getting a error at the line set arr = readfile( FileName )

I am trying to read an file into an array

and can not figure out what i am doing wrong

Thanks in advance for your assistance

 Dim FileName ' File Name to Process
 Call MainProcedure
 WScript.Quit

 Sub MainProcedure
   filename = "c:\print\check.bat"
   WScript.Echo( "Printing document in progress..." )
   WScript.Echo( "Filename ====> " & FileName )
   Dim arr, i
   i = 0
   set arr = readfile( FileName )
   For Each present In arr
     ' user = split(present,",")
     ' WScript.Echo user(0) & user(1) & user(2) & user(3) & user(4) & "|"
     i = i + 1
     WScript.Echo present & "|"
   Next
End Sub

Sub readfile(strFile)
  dim fs,objTextFile
  set fs=CreateObject("Scripting.FileSystemObject")
  If (fs.FileExists( strFile)) Then
     dim userArrayList
     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
  Else
     'Alert User
     WScript.Echo("File does not exist!")
     WScript.Quit()
  End If
end Sub

Upvotes: 0

Views: 1853

Answers (1)

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38745

Your

set arr = readfile( FileName )

implies that readfile() is a Function (returning an ArrayList). But you define

Sub readfile(strFile)
...
set readfile = userArrayList
...
end Sub

You may try to change this to

Function readfile(strFile)
...
set readfile = userArrayList
...
End Function

ADDED:

The task "Read a files' lines into an array" can be done in a much more simple way:

cscript fitoar.vbs
0 Option Explicit
1 Dim a : a = Split(CreateObject("Scripting.FileSystemObject").OpenTextFile("fitoar.vbs").ReadAll(), vbCrLf)
2 Dim l
3 For l = 0 To UBound(a)
4     WScript.Echo l, a(l)
5 Next
6

Upvotes: 4

Related Questions