【问题标题】:generate log for each day每天生成日志
【发布时间】:2017-10-23 19:05:07
【问题描述】:

我正在尝试使用此脚本为一天创建一个日志文件:

hostIp      = WScript.Arguments(0)
logfilename = WScript.Arguments(1)
Set fso     = CreateObject("Scripting.FileSystemObject")
Set Shell   = CreateObject("WScript.Shell")
'OpenTextFile Method requires a Const value
'(Over)Write = 2  Append = 8  
d = Day(Now) 
m = Month(Now)  
y = Year(Now)
myDateFormat= d & "-" & m & "-" & y 
Set logfile = fso.OpenTextFile(logfilename & " " & myDateFormat & ".log", 8, True)
shellstring = "%comspec% /c ping -t -f -l 32 -w 1000 " & hostIP
Set oExec   = Shell.Exec(shellstring)
WScript.Echo "Ping Error log With Timestamp - Ctrl + C to halt"
Do While oExec.StdOut.AtEndOfStream <> True
    pingline = Date & " " & Time & " " & oExec.StdOut.ReadLine
    'If InStr(pingline, "TTL=") = 0 Then
        logfile.WriteLine(pingline)
    'End If
Loop

我认为这很好,但我已经运行了 3 天,并且只有一个文件而不是 3 个文件。关于脚本有什么问题的任何想法? 顺便说一句,我用这一行在 CMD 上运行这个脚本:

FileName ip logname.log

【问题讨论】:

  • 您拥有的日志文件的名称是什么?
  • PingRemote 12-10-2017.log

标签: cmd vbscript windows-server-2008


【解决方案1】:

嗯...嗯,是的,您的日志文件不会因为您愿意而神奇地改变。您需要实际告诉您的代码来执行此操作。

由于您基本上是在运行一个无限循环(由于ping -t),您需要检查循环中的日期,并在日期更改时打开一个新文件:

d = Date - 1
Set logfile = Nothing
Do Until oExec.StdOut.AtEndOfStream
    pingline = Date & " " & Time & " " & oExec.StdOut.ReadLine
    If d <> Date Then
        If Not logfile Is Nothing Then logfile.Close
        d = Date
        myDateFormat = Day(d) & "-" & Month(d) & "-" & Year(d)
        Set logfile = fso.OpenTextFile(...)
    End If
    logfile.WriteLine(pingline)
Loop
logfile.Close

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-10
    • 2012-05-08
    • 2011-02-27
    • 2020-12-10
    • 2019-04-16
    • 2014-11-09
    相关资源
    最近更新 更多