我需要从URL获取日志文件,然后逐行打印日志文件。我可以这样成功获取文件:
Dim filePath,fileName
filePath = "http://localhost/osat/spawn_pi_logs"
fileName = "/Spawn.log"
FilePath = filePath & fileName
Set req = CreateObject("Msxml2.XMLHttp.6.0")
req.open "GET", FilePath, False
req.send
If req.Status = 200 Then
Response.Write "Found the file<br>"
Response.Write req.responsetext
End If
这段代码将我的日志文件的文本以一个巨大的丑陋的blob的形式写到屏幕上。我想遍历它,格式化它,搜索它,等等,然后用类似这样的代码写出来:
Do While Not TextStream.AtEndOfStream
Dim sLine
sLine = TextStream.readLine
sLine = sLine & "<br>"
Response.Write sLine
Loop
然而,我如何转换我的req对象(在req. responsestream有一个通用流)并将其转换为文本流?
您可以通过合并ADO流并写入responseBody
:
If req.Status = 200 Then
With CreateObject("ADODB.Stream")
.Type = 1 'adTypeBinary
.Open
.Write req.responseBody
.SaveToFile "c:myfile.txt"
.Close
End With
End If
然后你可以使用OpenTextFile()
打开你的文本文件,并读取它作为TextStream
。
但是,您可以将responseText
拆分为具有Split()
函数的数组,而不必担心保存和读取文本文件:
If req.Status = 200 Then
' Create a line array...
a = Split(req.responseText, vbCrLf)
For i = 0 To UBound(a)
' Process each line
Next
End If
谢谢PHD443322。下面的代码效果非常好:
a = Split(req.responsetext, vbCrLf)
for each x in a
response.write(x & "<br />")
next
关键不仅是Split命令,而且还使用VBScript的vbCrLf常量来设置分隔符。