如何使用此特定脚本创建每日日志文件

时间:2013-07-20 17:57:01

标签: vbscript

我找到了这个VBS脚本,它将来自Planeplotter(记录飞机的软件)的数据记录到CSV文件中。

现在我的问题是:如何修改此脚本以每天创建一个新的CSV文件。现在一切都塞满了一个档案。

' PlanePlotter script for logging active aircraft (including those received by sharing)
' Create a file in the Log Files directory
Dim fso, f1, ts
Const ForWriting = 2
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CreateTextFile ("B:\_Mijn Downloads en Werkmap\_Spotten\PlanePlotter\Log\pp_share_log.txt")
Set f1 = fso.GetFile("B:\_Mijn Downloads en Werkmap\_Spotten\PlanePlotter\Log\pp_share_log.txt")
Set ts = f1.OpenAsTextStream(ForWriting, True)

' Tap in to PlanePlotter
Dim MyObject
Set MyObject = GetObject(,"PlanePlotter.Document")

' Write all the aircraft data out to file every minute for 24 hours
while 1
  i = 0
  while i < MyObject.GetPlaneCount()
    hexc = MyObject.GetAllPlaneData(i,0)
    regc = MyObject.GetAllPlaneData(i,1)
    flnc = MyObject.GetAllPlaneData(i,2)
    latf = MyObject.GetAllPlaneData(i,3)
    latc = Cstr(latf)
    latc = Replace(latc,",",".")
    lonf = MyObject.GetAllPlaneData(i,4)
    lonc = CStr(lonf)
    lonc = Replace(lonc,",",".")
    altf = MyObject.GetAllPlaneData(i,5)
    altc = CStr(altf)
    altc = Replace(altc,",",".")
    hedf = MyObject.GetAllPlaneData(i,6)
    hedc = CStr(hedf)
    hedc = Replace(hedc,",",".")
    spdf = MyObject.GetAllPlaneData(i,7)
    spdc = CStr(spdf)
    spdc = Replace(spdc,",",".")
    recc = MyObject.GetAllPlaneData(i,8)
    recc = Replace(recc," ",",") ' put a comma half way through
    icao = MyObject.GetAllPlaneData(i,14)
    shar = MyObject.GetAllPlaneData(i,18)
    verr = MyObject.GetAllPlaneData(i,21)
    verc = CStr(verr)    
    planeinfo = recc + "," + hexc + "," + regc + "," + flnc + "," + icao + "," + altc + "," + latc + "," + lonc + "," + spdc + "," + hedc + "," + shar + "," + verc + ","
    ts.WriteLine (planeinfo)
    i = i + 1
  wend
  ts.WriteLine ("---------")
  Wscript.Sleep 180000 ' 60 Seconds
  n = n - 1
wend
ts.Close

1 个答案:

答案 0 :(得分:1)

此:

fso.CreateTextFile ("B:\_Mijn Downloads en Werkmap\_Spotten\PlanePlotter\Log\pp_share_log.txt")
Set f1 = fso.GetFile("B:\_Mijn Downloads en Werkmap\_Spotten\PlanePlotter\Log\pp_share_log.txt")
Set ts = f1.OpenAsTextStream(ForWriting, True)

可以简化为:

Set ts = fso.OpenTextFile("B:\_Mijn Downloads en Werkmap\_Spotten\PlanePlotter\Log\pp_share_log.txt", ForWriting, True)

如果你想每天创建一个新文件(不覆盖旧文件),你可能需要这样的东西:

Function LPad(s)
  LPad = Right("00" & s, 2)
End Function

Function FormatDate(d)
  FormatDate = Year(d) & "-" & LPad(Month(d)) & "-" & LPad(Day(d))
End Function

outputDir = "B:\_Mijn Downloads en Werkmap\_Spotten\PlanePlotter\Log"
basename  = "pp_share_log"
filename  = basename & "_" & FormatDate(Date)

Set ts = fso.OpenTextFile(fso.BuildPath(outputDir, filename), ForWriting, True)