问题
I have a VB script that is running on a server, several times a day. One of the things this script is doing is creating log files, stating the status of certain files, determined by the age of the files. I do this by first finding the oldest and newest file, and if the newest file is within a specific timestamp(5 min) the status is “OK”, IF something in the check goes wrong the status is changed to “ERROR” the status variable is string variable(should have been a Boolean, but that is for another day)
All this works properly, my issue is that before my scripts end I want it to take the last 10 lines from the newest file and write into the log file.
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
So here I am wrritting to the log file depending on the status.
What I know what is something like
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
The question is I have the tail.exe from http://unxutils.sourceforge.net/ but I am unsure how to call this on that specific file. the tail.exe file will be in the same folder as the script.
I've been looking to : http://www.visualbasicscript.com/Runing-an-executable-file-from-VBScript-m22.aspx for help but didnt work for me.
回答1:
errorStr = WScript.CreateObject("WScript.Shell").Exec( _
"tail -n 10 """ & sNewestFile & """" _
).StdOut.ReadAll
Or, if it is needed to execute the tail.exe
located in the script folder
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
来源:https://stackoverflow.com/questions/32349940/visual-basic-using-tail-on-a-specific-file-in-a-folder