2017-08-15 33 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