3

I need to read a text file in chunks so that I don't need show too much information on screen at once.

I need that the user can see the next chunk from result set as he click next button (each chunk are separated by next keyword).

I tried it showing in table but that was quite unreadable for user Read text file and show in table vbscript

My text file as given below :-

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

I can read the file as textStream but facing problem in reading it by portion.

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
Community
  • 1
  • 1
user2816085
  • 655
  • 4
  • 19
  • 2
    You can read the whole file, split it with `next` as delimiter, then show element by element from the resulting array. – omegastripes Mar 18 '16 at 13:01

1 Answers1

1

As he said omegastripes

You can read the whole file, split it with next as delimiter, then show element by element from the resulting array.

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
'*****************************************************************
Hackoo
  • 18,337
  • 3
  • 40
  • 70