0

我是 vbs 的新手,在 set arr = readfile( FileName ) 行出现错误

我正在尝试将文件读入数组

并且无法弄清楚我做错了什么

提前感谢你的帮助

 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
4

1 回答 1

4

您的

set arr = readfile( FileName )

意味着这readfile()是一个函数(返回一个 ArrayList)。但是你定义

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

您可以尝试将其更改为

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

添加:

任务“将文件的行读入数组”可以用更简单的方式完成:

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
于 2013-01-22T14:07:31.297 回答