如何在 vbscript 中检测文本文件 (EOF) 的结尾?

How can I detect the end of a text file (EOF) in vbscript?

我正在使用 VBscript 将文本文件中的信息排序为 excel sheet。我能够这样做,但只有一行文本被读取,循环控制似乎没有转到文本文件的下一行。我为此使用 AtEndOfStream,但正如我所说,我只得到一行输出。谁能帮我弄清楚如何执行程序直到文件末尾?

代码如下:

Set objExcel = CreateObject("Excel.Application")            'Bind to the Excel object
objExcel.Workbooks.Add                                      'Create a new workbook.
Sheet = 1                                                   'Select the first sheet
Set objSheet = objExcel.ActiveWorkbook.Worksheets(Sheet)    'Bind to worksheet. 
objSheet.Name = "ErrorSpreadsheet"                          'Name the worksheet
strExcelPath = "c:\scripts\ErrorSpreadsheet.xlsx"           'Set the save location

objSheet.Range("A1:E1").Font.Bold = True
objExcel.Columns(5).AutoFit()
objSheet.Cells(1, 1).Value = "Date"                         'Row 1 Column 1 (A)
objSheet.Cells(1, 2).Value = "Time"                         'Row 1 Column 2 (B)
objSheet.Cells(1, 3).Value = "Category"                     'Row 1 Column 3 (C)
objSheet.Cells(1, 4).Value = "Error"                        'Row 1 Column 4 (D)
objSheet.Cells(1, 5).Value = "Index"                        'Row 1 Column 5 (E)

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("c:\scripts\ErrorMsg.txt",1)

i = 0
r = 2
c = 1
j = 0
Do While NOT objFile.AtEndOfStream 
    Redim Preserve arrFileLines(10)
    arrFileLines(i) = objFile.ReadLine
    text = arrFileLines(j)
    'Dim a() 
    a = Split(text,";")
    For Each line In a
        objSheet.Cells(r,c).Value = a(i)
        i = i + 1
        c = c + 1
    Next
    j = j + 1
    r = r + 1
Loop


objFile.Close
objExcel.ActiveWorkbook.SaveAs strExcelPath
objExcel.ActiveWorkbook.Close
objExcel.Application.Quit

我现在编写的代码没有出现任何错误。在我的 excel 文件中,我有标题行和拆分文本文件的一行,但没有以下任何行。文本文件是这样写的:

       9/23;12:00;cat;error;236
       9/24;12:30;cat2;error;897
       9/24;4:06;cat5;error;23
       9/25;3:45;cat1;error;54
       9/26;1:09;cat6;error;18

所以我在 excel 中得到的输出是 Excel Output

谁能帮我弄清楚如何到达文本文件的末尾?

正如我在评论中提到的,你的问题出在你的变量上,因为你试图重用它们,但没有正确地使用它们。 如果您手动查看代码,您会看到,在第一次迭代中,您将 objFile.ReadLine 添加到 arrFileLines(0),然后将 arrFileLines(0) 存储到 text
但是随后您进入内部 For 循环并迭代 i,这将使 For 循环在拆分后保留为 4

第二次进入循环时,您会将 objFile.ReadLine 添加到 arrFileLines(4),然后将 arrFileLines(1)(为空)存储到 text。您不会得到任何错误,因为该数组具有固定维度并且在您完成遍历文件之前将在范围内,但您也不会得到任何结果。
这就是为什么我建议您使用不同的变量并避免重复使用的原因。
实际上,如果唯一的目的是将 CSV 中的值添加到 Excel sheet 中,您甚至不需要将 objFile.ReadLine 存储到 arrFileLines 中,因为您不是使用数组。直接加到Text就可以了。

所以,通过一些修改,比如变量重命名等,你最终会得到这样的东西:

' The rest of your code, Variables declarations and so forth

iRow = 2    ' Starting Row
iCol = 1    ' Starting Col

Do While Not objFile.AtEndOfStream
    currLine = objFile.ReadLine
    arrLine = Split(currLine, ";")
    For Each item In arrLine
        objSheet.Cells(iRow, iCol).Value = item
        iCol = iCol + 1
    Next
    iCol = 1    ' Reset for every row
    iRow = iRow + 1
Loop

' The rest of your code

为什么你要单独读取每一行,现在你可以用 objFile.ReadAll 一次将整个文件读入一个缓冲区?通过这种方式 1) 您立即知道 EOF(= 缓冲区的长度),2) 您可以轻松地 "read" 缓冲区中的每一行,3) 您 "read" 这些行比读取磁盘快得多,并且4)你避免过度使用磁盘,尤其是。如果文件很长!