微调vbscript

时间:2017-08-15 08:34:12

标签: vbscript

我正在编写一个VBScript来传回日期/时间值(特别是在凌晨2:00之前获取最后一天的值)。是否有任何微调而不是将值传递给另一个批处理并使用Batch1调用vbscript然后使用batch2(在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 个答案:

答案 0 :(得分: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
相关问题