1

I am writing a VBScript to pass back the date/time value (especially before 2:00 AM to get last day value). Is there any fine tuning instead of pass the value to another batch and use the Batch1 to call vbscript and then the batch2 (created in vbscript)? Thanks a lot

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  
Raymond
  • 11
  • 1

1 Answers1

0

I'm not sure if you want to run a batch script directly from VBScript, but in case that option is available, you don't need to write to generate a file at all - you can pass in the date and other info using command-line parameters.

In the example below, I simplified your date code and then passed some fields into a batch file, which will echo them back to VBScript to show in a message box. You can adapt for your needs.

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%

Output (for today, after 2am):

Year 2017 Month Aug Day 15
BoffinBrain
  • 6,337
  • 6
  • 33
  • 59
  • 1
    Avoid [re-inventing the wheel](https://msdn.microsoft.com/en-us/library/58f13257). – Ansgar Wiechers Aug 15 '17 at 12:33
  • @AnsgarWiechers Thanks. I'm pleasantly surprised VBS even has such a feature, given how badly it lacks in other areas like formatting dates! – BoffinBrain Aug 15 '17 at 14:16
  • As there was some old-school batch (commandline batch) which i dont want to amend. therefore, i need a additional scripts to calculate the day vale. in the past time, that script used batch (.bat) to process several operation, and now, the job will pass thru 00:00, therefore, the day value will be changed, but we need the last working day (or last day) day value. – Raymond Aug 16 '17 at 09:43
  • Well, if you can't modify the original batch file then I don't think there's going to be any way you can get those CMD variables set. You cannot do `shell.Exec("set Year=" & runYear)` for example... That only seems to work in the context of a real command line. – BoffinBrain Aug 16 '17 at 12:27
  • Does the VBS script run at the same time (immediately before) the batch script? If you were just able to modify the very top of your batch file to include the `set Year=%1 ...` lines, then you could just run the VBS and it would magically work. – BoffinBrain Aug 16 '17 at 12:29