我有一个具有以下结构的文本文件:-

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


我必须使用vbscript在网页上以表格格式显示此信息。
该表应为:-

+------+--------------+--------------+--------------+-----------+
| Path | AccountName1 | AccountName2 | QueryAccount | QueryName |
+------+--------------+--------------+--------------+-----------+
|C:\...| AbcPos       | dblLayer     | qskxyz       | qixyz     |
+------+--------------+--------------+--------------+-----------+
|C:\...| AbcPos       | dblLayer     | qskxyz       | abc       |
+------+--------------+--------------+--------------+-----------+
|C:\...| AbcPos       | dblLayer     | qskxyz       | def       |
+------+--------------+--------------+--------------+-----------+
|C:\3..| AbcPos       | dblLayer     | qskxyz       | qixyz     |
+------+--------------+--------------+--------------+-----------+
|C:\3..| Names        | prelLayer    | qskxyz       | abc       |
+------+--------------+--------------+--------------+-----------+
|C:\3..| AbcPos       | dblLayer     | searchTerm   | myName1   |
+------+--------------+--------------+--------------+-----------+


目前,我只是用vbscript阅读文本文件,但需要以表格格式显示它。

Set dict = CreateObject("Scripting.Dictionary")
Set file = fso.OpenTextFile ("c:\test.txt", 1)
row = 0
Do Until file.AtEndOfStream
  line = file.Readline
  dict.Add row, line
  row = row + 1
Loop

file.Close


由于无法从我的环境中进行复制,因此无法在此处发布完整的代码。

最佳答案

为了使您开始解析(一个和唯一的)输入文件:


遍历行(无需将它们加载到内存中)
使用“下一个”检测“记录结束”
在“->”上分割以获取键值对
通过连接将有趣的值存储在数组中以简化格式/标记


如:

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


输出:

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|


抱歉,ASP无法为您提供帮助。

关于vbscript - 读取文本文件并在表vbscript中显示,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36060599/

10-13 02:37