Visual Basic在文件夹中的特定文件上使用尾部 [英] Visual Basic using tail on a specific file in a folder

查看:65
本文介绍了Visual Basic在文件夹中的特定文件上使用尾部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个VB脚本,每天在服务器上运行几次.该脚本要做的一件事是创建日志文件,说明某些文件的状态,该状态由文件的生存期决定. 我首先查找最旧和最新的文件,如果最新文件在特定的时间戳记(5分钟)内,则状态为"OK",如果检查中出现问题,则状态会更改为"ERROR" 状态变量是字符串变量(应该是布尔值,但这又是一天)

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)

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

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.

我所知道的是什么

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文件将与脚本位于同一文件夹中.

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.

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

I've been looking to : http://www.visualbasicscript.com/Runing-an-executable-file-from-VBScript-m22.aspx for help but didnt work for me.

推荐答案

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

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

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

这篇关于Visual Basic在文件夹中的特定文件上使用尾部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆