用于在chunck中读取文件的vbscript

时间:2016-03-18 12:53:13

标签: vbscript

我需要以块的形式阅读文本文件,以便我不需要立即在屏幕上显示太多信息。

我需要用户在点击下一个按钮时可以看到结果集中的下一个块(每个块由下一个关键字分隔)。

我尝试在表格中显示但这对用户来说非常难以理解 Read text file and show in table vbscript

我的文本文件如下: -

C:\Users\abc\Desktop\New Folder\sample.txt
AccountName->AbcPos
AccountName->dblLayer
queryAccount->qskxyz
queryName->qixyz
queryName->abc
queryName->def

next

C:\Users\abc\Desktop\New Folder\New folder\sample3.txt
AccountName->AbcPos
AccountName->dblLayer
queryAccount->qskxyz
queryName->qixyz
AccountName->Names
AccountName->prelLayer
queryAccount->serchTerm
queryName->myName1

next

C:\Users\abc\Desktop\New Folder\sample1.txt
AccountName->AbcPos
AccountName->dblLayer
queryAccount->qskxyz
queryName->qixyz

next

C:\Users\abc\Desktop\New Folder\sample2.txt
AccountName->AbcPos
AccountName->dblLayer
queryAccount->qskxyz
queryName->qixyz
queryName->abc
queryName->def
AccountName->Names
AccountName->prelLayer
queryAccount->serchTerm
queryName->myName1

next

我可以将文件作为textStream读取,但在按部分阅读时遇到问题。

Option Explicit

Const csSep = "->"

Dim oFS : Set oFS = CreateObject("Scripting.FileSystemObject")
Dim oTS : Set oTS = oFS.OpenTextFile("..\data\36060599.txt")

ReDim aData(4)
Do Until oTS.AtEndOfStream
   Dim sLine  : sLine  = Trim(oTS.ReadLine())
   Dim sValue : sValue = ""
   If InStr(sLine, csSep) Then sValue = Split(sLine, csSep)(1)
   Select Case True
      Case ":" = Mid(sLine, 2, 1) ' the Path
        aData(0) = sLine
      Case "AccountName" = Left(sLine, 11)
        aData(2 + IsEmpty(aData(1))) = sValue
      Case "queryAccount" = Left(sLine, 12)
        aData(3 + IsEmpty(aData(1))) = sValue
      Case "queryName" = Left(sLine, 9)
        aData(4 + IsEmpty(aData(1))) = sValue
      Case "next" = sLine ' End Of Record
'       WScript.Echo "<tr><td>" & Join(aData, "</td><td>") & "</td></tr>"
        WScript.Echo "|" & Join(aData, "|") & "|"
        ReDim aData(4)
   End Select
Loop
oTS.Close

1 个答案:

答案 0 :(得分:1)

正如他所说 omegastripes

  

您可以读取整个文件,然后将其拆分为下一个作为分隔符   从结果数组中按元素显示元素。

Option Explicit
Dim strFile,Contents,arrLines,i 
strFile = "NextFile.txt"
Contents = ReadFile(strFile,"all")
arrLines = Split(Contents,"next")
For i=LBound(arrLines) to UBound(arrLines)
    If arrLines(i) <> "" Then
        MsgBox arrLines(i)
    End If  
Next
'*********************************************
Function ReadFile(path,mode)
    Const ForReading = 1
    Dim objFSO,objFile,i,strLine
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile(path,ForReading)
    If mode = "byline" then
        Dim arrFileLines()
        i = 0
        Do Until objFile.AtEndOfStream
            Redim Preserve arrFileLines(i)
            strLine = objFile.ReadLine
            strLine = Trim(strLine)
            If Len(strLine) > 0 Then
                arrFileLines(i) = strLine
                i = i + 1
                ReadFile = arrFileLines
            End If  
        Loop
        objFile.Close
    End If
    If mode = "all" then
        ReadFile = objFile.ReadAll
        objFile.Close
    End If
End Function
'*****************************************************************