每天生成日志
generate log for each day
我正在尝试使用此脚本创建一天的日志文件:
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
嗯...嗯,是的,您的日志文件不会因为您愿意而神奇地改变。您需要实际告诉您的代码这样做。
因为你基本上是 运行 一个无限循环(由于 ping -t
)你需要检查日期 inside 循环并打开一个新的日期更改时的文件:
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
我正在尝试使用此脚本创建一天的日志文件:
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
嗯...嗯,是的,您的日志文件不会因为您愿意而神奇地改变。您需要实际告诉您的代码这样做。
因为你基本上是 运行 一个无限循环(由于 ping -t
)你需要检查日期 inside 循环并打开一个新的日期更改时的文件:
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