读取文本文件并在表vbscript中显示

时间:2016-03-17 12:29:36

标签: vbscript asp-classic

我有一个文本文件,结构如下: -

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

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

2 个答案:

答案 0 :(得分:1)

为了让你开始解析你的(唯一的)输入文件:

  1. 循环上线(无需将其加载到内存中)
  2. 使用" next"检测记录结束'
  3. 拆分" - >"获得键值对
  4. 将有趣的值存储在数组中,以便通过Join
  5. 轻松进行格式化/标记

    如:

    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问题。

答案 1 :(得分:0)

我的实施就像: -

Set mytable=document.CreateElement("table")
set thead = document.createElement("thead")
set tr=document.createElement("tr")
set th1=document.createElement("th")

th1.setattribute "colSpan","4" 
tr.appendChild th1
thead.appendChild tr
set tr2= document.createElement("tr")
set th1= document.createElement("th")   
set th2= document.createElement("th")
set th3= document.createElement("th")
set th4= document.createElement("th")
set th5= document.createElement("th")
th1.innerText="AccountName1"
th2.innerText="AccoutnName2"
th3.innerText="QueryAccount"
th4.innerText="QueryName"
th5.innerText="Path/Location"
tr2.appendChild th1
tr2.appendChild th2
tr2.appendChild th3
tr2.appendChild th4
tr2.appendChild th5
thead.appendChild tr2
mytable.appendChild thead

.......................
.......................
For Each thing in Fls
......................
........................

set td1 = document.createElement("td")
set td2 = document.createElement("td")
set td3 = document.createElement("td")
set td4 = document.createElement("td")
set td5 = document.createElement("td")

if condition

set tr3=document.createElement("tr")
td1.innerText=nodeinfo(0)
td2.innerText=nodeinfo(1)
td3.innerText=nodeinfo(2)
td4.innerText=node.text
td5.innerHtml="<a href=" & "'" & thing.Path & "'" & ">" & thing.Path &"</a>"

tr3.appendChild td1
tr3.appendChild td2
tr3.appendChild td3
tr3.appendChild td4
tr3.appendChild td5
tbod.appendChild tr3
elseif i=2 then
set tr3=document.createElement("tr")
td1.innerText="--"
td2.innerText=nodeinfo(0)
td3.innerText=nodeinfo(1)
td4.innerText=node.text
td5.innerHtml="<a href=" & "'" & thing.Path & "'" & ">" & thing.Path &"</a>"
tr3.appendChild td1
tr3.appendChild td2
tr3.appendChild td3
tr3.appendChild td4
tr3.appendChild td5
tbod.appendChild tr3
elseif i=1 then
set tr3=document.createElement("tr")
td1.innerText="--"
td2.innerText="--"
td3.innerText=nodeinfo(0)
td4.innerText=node.text
td5.innerHtml="<a href=" & "'" & thing.Path & "'" & ">" & thing.Path &"</a>"
tr3.appendChild td1
tr3.appendChild td2
tr3.appendChild td3
tr3.appendChild td4
tr3.appendChild td5
tbod.appendChild tr3