Visual Basic 在文件夹中的特定文件上使用 tail

Visual Basic using tail on a specific file in a folder

我有一个 VB 脚本,它在服务器上 运行 一天几次。这个脚本正在做的事情之一是创建日志文件,说明某些文件的状态,由文件的年龄决定。 我首先找到最旧和最新的文件,如果最新的文件在特定时间戳(5 分钟)内,则状态为“OK”,如果检查中出现问题,则状态更改为“ERROR” 状态变量是字符串变量(应该是布尔值,但那是另一天)

所有这些工作正常,我的问题是在我的脚本结束之前,我希望它从最新文件中取出最后 10 行并写入日志文件。

If IsNull(sNewestFile) Then
        status = "ERROR"
        objLogFile.writeLine "Directory: " & VbTab & VbTab & VbTab & a_dirpath & vbCrLf & filespecstr & vbCrLf & "Status " & vbTab & vbTab & VbTab & VbTab & status & vbTab & vbTab & "No files found in directory."
        objLogFile.writeLine "Newest File: " & VbTab & VbTab & VbTab & ""  & vbCrLf & "Alert Seconds " & VbTab & VbTab & VbTab & a_seconds & vbCrLf &"Oldest File: "&vbTab & vbTab &vbTab
        objLogFile.writeline "Error Message: " & vbTab & vbTab & vbTab & "De 10 linjer fra Error loggen skal jo så være her." & VbTab & VbTab & VbTab & status & vbTab & vbTab
    Else
        diff = DateDiff("s",dPrevDate,Now)
        If CLng(diff) > CLng(a_seconds) Then
            status = "ERROR"
            objLogFile.writeLine "Directory: " & VbTab & VbTab & VbTab & a_dirpath & vbCrLf & filespecstr & vbCrLf & "Status " & VbTab & VbTab & VbTab & status & vbTab & vbTab & "Newest file is to old."
            objLogFile.writeLine "Newest File: " & VbTab & VbTab & VbTab & sNewestFile &vbTab& dPrevDate  & vbCrLf & "Alert Seconds " & VbTab & VbTab & VbTab & a_seconds & vbCrLf &"Oldest File: "&vbTab & vbTab &vbTab & sOldestFile &vbTab& dOldPrevDate  & vbCrLf
            objLogFile.writeline "Error Message: " & vbTab & vbTab & vbTab & "De 10 linjer fra Error loggen skal jo så være her." & VbTab & VbTab & VbTab & status & vbTab & vbTab              
        Else
            status = "OK"
            objLogFile.writeLine "Directory: " & VbTab & VbTab & VbTab & a_dirpath & vbCrLf & filespecstr
            objLogFile.writeline "Status: " & VbTab & VbTab & VbTab & status 
            objLogFile.writeline "Alert Seconds: " & VbTab & VbTab & VbTab & a_seconds 
            objLogFile.writeLine "Newest File: " & vbCrLf & VbTab & sNewestFile & vbCrLf & VbTab & "TimeStamp: " & VbTab & VbTab & dPrevDate & vbCrLf
            objLogFile.writeline "Oldest File: "& vbCrLf & VbTab & sOldestFile & vbCrLf & VbTab & "TimeStamp: " & VbTab & VbTab & dOldPrevDate & vbCrLf 
        End If
    End if
Else
    status = "ERROR"
    objLogFile.writeLine "Directory: " & VbTab & VbTab & VbTab & a_dirpath  & vbCrLf & "Status " & VbTab & VbTab & VbTab & status & vbTab & vbTab & "Directory does not exist."
    objLogFile.writeLine "Newest File: " & VbTab & VbTab & VbTab & ""  & vbCrLf & "Alert Seconds " & VbTab & VbTab & VbTab & a_seconds & vbCrLf &"Oldest File: "&vbTab & vbTab &vbTab &""
    LogError("The '"&a_dirpath &"' does not exist. The directory will not be checked." )
End If

所以我在这里根据状态写入日志文件。

我所知道的是什么

Try
    if(status = "ERROR") then
        'It is the NewestFile I want to run TAIL.exe on to get the last 10 lines of text.
        'how do I do this?

    End if
    if (status = "ERROR") then
    objLogFile.writeline "Error Message: " & vbCrLf & 
'Using a string variable with the 10 lines of code
    else
        objLogFile.writeline "Error Message: No error has been detected."
    End If  
End Try

问题是我有来自 http://unxutils.sourceforge.net/ 的 tail.exe,但我不确定如何在那个特定文件上调用它。 tail.exe 文件将与脚本位于同一文件夹中。

我一直在寻找 : http://www.visualbasicscript.com/Runing-an-executable-file-from-VBScript-m22.aspx 寻求帮助,但对我没有帮助。

errorStr = WScript.CreateObject("WScript.Shell").Exec( _ 
               "tail -n 10 """ & sNewestFile & """" _ 
           ).StdOut.ReadAll

或者,如果需要执行脚本文件夹中的tail.exe

With WScript.CreateObject("Scripting.FileSystemObject")
    tail = .BuildPath(.GetFile(WScript.ScriptFullName).ParentFolder.Path, "tail.exe")
End With 

errorStr = WScript.CreateObject("WScript.Shell").Exec( _ 
               """" & tail & """ -n 10 """ & sNewestFile & """" _ 
           ).StdOut.ReadAll