2014-01-07 26 views
0

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

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

对于实施例 -

在子StartServers的,所述DatenTime变量具有一定时间的值(例如:2014-01-16-16-10-01.50)和后120秒的睡眠时间,所述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  = "<[email protected]>" 
      .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 

回答

0

你只需要创建子高于变量并存储日期/时间。然后参考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  = "<[email protected]>" 
      .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 
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确定自上次完全第二已经过的毫秒数。