2017-08-15 35 views
1

我写一个VBScript来传回的日期/时间值(尤其是前2:00 AM拿到最后一天的值)。是否有任何微调而不是将值传递给另一批次,并使用Batch1调用vbscript,然后使用batchbs2(在vbscript中创建)?非常感谢微调的VBScript

dim dateMonth, dateDay, dateYear, dateYY, dateMMM, MM, pDateDay 
'Check Time 
if hour(now) < 2 then 'Before 2AM, count as last working day 
    dateMonth = Month(dateadd("d",-1,now)) 
    dateDay  = Day(dateadd("d",-1,now)) 
    dateYear = Year(dateadd("d",-1,now)) 
    dateYY  = right(year(dateadd("d",-1,now)),2) 
    TimeHH  = Hour(now) 
    TimeMM  = Minute(now) 
else 
    dateMonth = Month(now) 
    dateDay  = Day(now) 
    dateYear = Year(now) 
    dateYY  = right(year(now),2) 
    TimeHH  = Hour(now) 
    TimeMM  = Minute(now) 
end if 

MM = Array("","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec") 
dateMMM = mm(dateMonth) 
if dateMonth < 10 then 
    dateMonth = "0" & dateMonth 
end if 
If dateDay < 10 then 
    dateDay = "0" & dateDay 
End if 
If TimeHH < 10 then 
    TimeHH = "0" & TimeHH 
End if 
If TimeMM < 10 then 
    TimeMM = "0" & TimeMM 
End if 


Set objFSO=CreateObject("Scripting.FileSystemObject") 

' Create Log file 
Dim oFSO, oTxtFile, curDir 
Set oFSO = CreateObject("Scripting.FileSystemObject")  
curDir = oFSO.GetAbsolutePathName(".") 
strFile = "\datetime.bat" 
If oFSO.FileExists(curDir & strFile) then 
     oFSO.DeleteFile curDir & strFile 
end if 

strValue = "SET Date_MMDD=" & dateMonth & dateDay 
strValue = strValue & vbcrlf & "SET Date_MM=" & dateMonth 
strValue = strValue & vbcrlf & "SET Date_MMM=" & dateMMM 
strValue = strValue & vbcrlf & "SET Date_DD=" & dateDay 
strValue = strValue & vbcrlf & "SET Date_HHMM=" & TimeHH & TimeMM 
strValue = strValue & vbcrlf & "SET Time_HH=" & TimeHH 
strValue = strValue & vbcrlf & "SET Time_MM=" & TimeMM 


Set oTxtFile = oFSO.CreateTextFile(curDir & strFile) 
oTxtFile.writeline(strValue) 

wscript.echo strValue 

set oTxtFile = nothing 
set oFSO = nothing 
+1

你有什么需要在批处理脚本做你不能直接在VBScript中做? – BoffinbraiN

回答

0

我不知道,如果你想直接从VBScript运行一个批处理脚本,但在情况下选项可用,你不需要写入生成一个文件在所有 - 你可以使用命令行参数传递日期和其他信息。

在下面的例子中,我简化您的日期代码,然后传递某些字段到一个批处理文件,它会回应他们回到VBScript中的一个消息框来显示。你可以适应你的需求。

test.vbs:

Option Explicit 

Dim runDate : runDate = Now() 
If Hour(runDate) < 2 Then runDate = DateAdd("d", -1, runDate) 

Dim runYear : runYear = Year(runDate) 
Dim runMonth : runMonth = MonthName(Month(runDate), True) 
Dim runDay : runDay = Day(runDate) 
' Etc... 

Dim parameters : parameters = Join(Array(runYear, runMonth, runDay), " ") 

' See https://stackoverflow.com/a/45284140/534406 for the below code 

Const WshRunning = 0 
Const WshFinished = 1 
Const WshFailed = 2 

Dim shell : Set shell = CreateObject("WScript.Shell") 
Dim exec : Set exec = shell.Exec("test.bat " & parameters) 

While exec.Status = WshRunning 
    WScript.Sleep 50 
Wend 

Dim output 

If exec.Status = WshFailed Then 
    output = exec.StdErr.ReadAll 
Else 
    output = exec.StdOut.ReadAll 
End If 

WScript.Echo output 

test.bat的

@echo off 
set Year=%1 
set Month=%2 
set Day=%3 
echo Year %Year% Month %Month% Day %Day% 

输出(今日凌晨2点后):

Year 2017 Month Aug Day 15 
+1

避免[重新发明轮子(https://msdn.microsoft.com/en-us/library/58f13257)。 –

+0

@AnsgarWiechers谢谢。令人惊喜的是,VBS甚至有这样一个功能,因为它在格式化日期等其他方面的功能有多糟糕! – BoffinbraiN

+0

由于我不想修改一些旧式批次(命令行批次)。因此,我需要一个额外的脚本来计算天价。 在过去的时间里,该脚本使用批处理(.bat)来处理一些操作,而现在,这项工作将直通00:00,因此,当天值将被改变,但我们需要的最后一个工作日(或最后一天)日价值。 – Raymond