gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:12:22 25 4
gpt4 key购买 nike

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

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

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

最佳答案

让您开始解析您的(唯一的)输入文件:

  1. 循环各行(无需将它们加载到内存中)
  2. 使用“next”检测“记录结束”
  3. 按“->”拆分以获得键值对
  4. 将感兴趣的值存储在数组中,以便通过 Join 轻松格式化/标记

如:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com