Read text file and show in table vbscript

前端 未结 2 490
攒了一身酷
攒了一身酷 2021-01-14 17:27

I have a text file with following structure :-

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


        
2条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-14 18:09

    To get you started wrt the parsing of your (one and only) input file:

    1. Loop over the lines (no need to load them into memory)
    2. Use "next" to detect 'end of record'
    3. Split on "->" to get key-value-pairs
    4. Store the interesting values in an array to make format/markup easy via Join

    As in:

    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 "" & Join(aData, "") & ""
            WScript.Echo "|" & Join(aData, "|") & "|"
            ReDim aData(4)
       End Select
    Loop
    oTS.Close
    

    output:

    cscript 36060599.vbs
    |C:\Users\abc\Desktop\New Folder\sample.txt|AbcPos|dblLayer|qskxyz|def|
    |C:\Users\abc\Desktop\New Folder\New folder\sample3.txt|AbcPos|prelLayer|serchTerm|myName1|
    |C:\Users\abc\Desktop\New Folder\sample1.txt|AbcPos|dblLayer|qskxyz|qixyz|
    |C:\Users\abc\Desktop\New Folder\sample2.txt|AbcPos|prelLayer|serchTerm|myName1|
    

    Can't help you with ASP, sorry.

提交回复
热议问题