Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
223 views
in Technique[技术] by (71.8m points)

html - How to obtain last three lines content of a log file using VBScript function

Can anyone suggest me a VBscript function to get the last 3 lines of a text document (for eg: log.txt ? Below is my code which can fetch and display the entire log on my screen but I want to get only last 3 lines of the log file named log.txt.

<script type="text/Vbscript">
Option Explicit
Dim File
File = "C:\test.txt"
'***********************************************************
Sub LoadMyFile()
    myDiv.innerHTML = LoadFile(File)
End Sub
'***********************************************************
Function LoadFile(File)
    On Error Resume Next
    Dim fso,F,ReadMe,Tab,i,paragraphe
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set F = fso.OpenTextFile(File,1)
    LoadFile = Err.Number
    If Err.Number <> 0  Then
        MsgBox Err.Description,16," Error"
        Exit Function
    End If
    ReadMe = F.ReadAll
    Tab = split(ReadMe,vbcrlf)
    For i = lbound(Tab) to ubound(Tab)
        paragraphe=paragraphe & Tab(i) & "<br>"
    Next
    LoadFile = paragraphe
End Function
</script>

Code not working@Steve

    <html>
    <script type="text/Vbscript">
    Option Explicit
    Dim File
    File = "C:\test.txt"
    '***********************************************************
    Sub LoadMyFile()
        myDiv.innerHTML = LoadFile(File)
    End Sub
    ************************************************************
    Function CheckProcesses()
    dim startLine
On Error Resume Next
    Dim fso,F,ReadMe,Tab,i,paragraphe
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set F = fso.OpenTextFile(File,1)
    LoadFile = Err.Number
    If Err.Number <> 0  Then
        MsgBox Err.Description,16," Error"
        Exit Function
    End If
    ReadMe = F.ReadAll
    Tab = split(ReadMe,vbcrlf)
    For i = lbound(Tab) to ubound(Tab)
        paragraphe=paragraphe & Tab(i) & "<br>"
    Next
    if ubound(Tab) > 2 Then
       startLine = ubound(Tab) - 2
    else
       startLine = 0
    end if
    For i = startLine to ubound(Tab)
        paragraphe=paragraphe & Tab(i) & "<br>"
    Next
    LoadFile = paragraphe
    End Function
    </script>
     <input type="button" name="Log" id="Start" value="Log Dctm" onclick="CheckProcesses()"></html>

Thanks and regards Deb

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Another solution that avoids memory exhaustion with large files:

filename = "C:path	oyour.txt"
numlines = 3

Set fso = CreateObject("Scripting.FileSystemObject")

'create and initialize ring buffer
ReDim buf(numlines-1)
For n = 0 To UBound(buf)
  buf(n) = Null
Next
i = 0

'read lines into ring buffer
Set f = fso.OpenTextFile(filename)
Do Until f.AtEndOfStream
  buf(i) = f.ReadLine
  i = (i+1) Mod numlines
Loop
f.Close

'output ringbuffer content (skip null values)
For n = 1 To numlines
  If Not IsNull(buf(i)) Then WScript.Echo buf(i)
  i = (i+1) Mod numlines
Next

The array buf in combination with the index variable i and the modulo operation serves as a ring buffer containing the last lines read from the file (numlines at most).

At the end of the second loop (the one reading the input file), the index i points towards the array field after the one containing the last line read from the file, i.e. the beginning of the buffer.

The Null values from the array initialization let the output routine "slide" to the first content line (or the end of the buffer) if less than numlines lines were read from the file. The variable n in the output loop is just a counter so that the numlines elements from the ring buffer are read starting at index i and ending at index i-1 (modulo wrapping).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...