vb脚本中的时间变量

时间:2014-01-07 01:22:33

标签: date time vbscript

我试图在多个地方使用VB脚本中的变量。每次调用变量时都会计算此变量。

脚本可以使用变量的初始值吗?

例如 -

在Sub StartServers 中,DatenTime变量具有特定时间值(例如:2014-01-16-16-10-01.50),并且在120秒休眠时间之后,Subn中的DatenTime值例程SendMail添加了120秒的值(例如:2014-01-16-16-12-01.50)导致不同的时间戳,并且由于找不到文件名而没有发送附件。

提前感谢您的回答。如果需要更多详细信息,请与我们联系。

=============================

DatenTime = "%date:~10,4%-%date:~4,2%-%date:~7,2%_%time:~0,2%_%time:~3,2%_%time:~6,5%"

Sub StartServers
    wshShell.Run "E:\Automation\bin\queryhpe.cmd > E:\Automation\log\query_hpe_"&DatenTime&".log"
    WScript.Sleep 120000
End Sub

Sub SendMail
    On Error Resume Next
.
.
.
.
.
           Set .Configuration = iConf
           .To       = sEmailList.ReadLine
           .From     = "<admin@example.com>"
           .Subject  = "STAGE: Querying Windows  Services at " & Time & " on " & Date
           .Textbody = "STAGE: Querying Windows  executed at " & Time & " on " & Date & " by " & sWho & "." & vbcrlf & vbcrlf & "Pls find the info on following location " &  "Pls Review attached logs for detailed information"
           .AddAttachment "E:\Automation\log\query_hpe_"&DatenTime&".log"
           .Send
       End With
    Loop
End Sub

2 个答案:

答案 0 :(得分:1)

不要使用环境变量在VBScript中构造时间戳字符串。请改用相应的VBScript函数:

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

t  = Now
DatenTime = Year(t) & "-" & LPad(Month(t)) & "-" & LPad(Day(t)) _
  & "_" & LPad(Hour(t)) & "_" & LPad(Minute(t)) & "_" & LPad(Second(t)) _
  & "." & LPad(Left(Timer * 1000 Mod 1000, 2))

表达式Timer * 1000 Mod 1000确定自上​​一个完整秒数以来经过的毫秒数。

答案 1 :(得分:0)

您只需在Sub上方创建一个变量并将日期/时间存储到它。然后在Sub。

中引用这些变量
DatenTime = "%date:~10,4%-%date:~4,2%-%date:~7,2%_%time:~0,2%_%time:~3,2%_%time:~6,5%"

Dim StartDate, StartTime
StartDate = Date
StartTime = Time

Sub StartServers
    wshShell.Run "E:\Automation\bin\queryhpe.cmd > E:\Automation\log\query_hpe_"&DatenTime&".log"
    WScript.Sleep 120000
End Sub

Sub SendMail
    On Error Resume Next
.
.
.
.
.
           Set .Configuration = iConf
           .To       = sEmailList.ReadLine
           .From     = "<admin@example.com>"
           .Subject  = "STAGE: Querying Windows  Services at " & StartTime & " on " & StartDate
           .Textbody = "STAGE: Querying Windows  executed at " & StartTime & " on " & StartDate & " by " & sWho & "." & vbcrlf & vbcrlf & "Pls find the info on following location " &  "Pls Review attached logs for detailed information"
           .AddAttachment "E:\Automation\log\query_hpe_"&DatenTime&".log"
           .Send
       End With
    Loop
End Sub