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

若如初见. 提交于 2019-12-13 09:04:23

问题


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


回答1:


Given an array of lines (Tab), the last n lines to display start from UBound(Tab) - n + 1 and end with UBound(Tab). You should test for 'less than n lines in Tab' and for 'is last line of Tab empty (trailing EOL)'.




回答2:


Another solution that avoids memory exhaustion with large files:

filename = "C:\path\to\your.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).




回答3:


I am not able to test this, but If you know the UBound of the variable Tab, then the last three lines are UBound(tab)-2, UBound(tab)-1 and UBound(tab).

For i = ubound(Tab) - 2 to ubound(Tab)
    paragraphe=paragraphe & Tab(i) & "<br>"
Next

Of course this requires that you have at least 3 lines in your log file, so, perhaps a little check should be done before entering the loop

dim startLine
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



回答4:


Another solution

You can use this function :

Function ExtractLinesFromTextFile(ByRef TextFile, ByRef FromLine, ByRef ToLine)

Option Explicit
Dim Title,FromLine,ToLine,fso,Readfile,strBuff,InputFile,TotalNbLines
Title = "Extract Lines From TextFile © Hackoo 2014"
InputFile = "c:\test.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
Set Readfile = Fso.OpenTextFile(InputFile,1)
strBuff = Readfile.ReadAll
TotalNbLines = Readfile.Line
Readfile.Close
MsgBox "The total number of lines in this file """& InputFile &""" = "& TotalNbLines,VbInformation,Title
'To extract the 3 last lines
MsgBox ExtractLinesFromTextFile(InputFile,TotalNbLines - 2,TotalNbLines),64,Title
'*********************************************************************************************************
Public Function ExtractLinesFromTextFile(ByRef TextFile, ByRef FromLine, ByRef ToLine) '<-- Inclusive
    Const TristateUseDefault = -2 'To Open the file using the system default.
    On Error Resume Next
    If FromLine <= ToLine Then
        With CreateObject("Scripting.FileSystemObject").OpenTextFile(TextFile,1,true,TristateUseDefault)
            If Err.number <> 0 Then
                MsgBox err.description,16,err.description
                Exit Function
            Else
                Do Until .Line = FromLine Or .AtEndOfStream
                    .SkipLine
                Loop
                Do Until .Line > ToLine Or .AtEndOfStream
                    ExtractLinesFromTextFile = ExtractLinesFromTextFile & (.ReadLine & vbNewLine)
                Loop
            End If
        End With
    Else
        MsgBox "Error to Read Line in TextFile", vbCritical,"Error to Read Line in TextFile"
    End If
End Function
'*********************************************************************************************************


来源:https://stackoverflow.com/questions/24035730/how-to-obtain-last-three-lines-content-of-a-log-file-using-vbscript-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!