我有一个在服务器上运行的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寻求帮助,但对我没有用。
答案 0 :(得分:1)
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