|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
IntroductionDid you ever tried to use date and time in logfiles or batch/command files. SolutionTe solution is very simpel. Copy the following code in a file called RunNow.cmd
'***********************************************
'
' RunNow.vbs
' (c) 2008 Computech.
' Initialy Version 2003.
' 2/2/2008 Extra Functionality
' Written by Peter Verijke
'
'***********************************************
Dim dDay, dLDOM, dLDOY, dLFOW, dLFOM
Dim ArgObj
Dim BatchFile
Dim sCommand
Dim WshShell ' as object
Dim objEnv ' as collection
Dim sGetMyVar ' as string
' Get the Arguments object
Set ArgObj = WScript.Arguments
' Test to make sure there is at least one command line arg - the command
If ArgObj.Count < 1 Then
DisplayHelpMessage
WScript.Quit
End If
BatchFile = ArgObj(0)
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objEnv = WshShell.Environment("PROCESS")
dNow = Now()
dLDOM = DateSerial(Year(dNow),Month(dNow) + 1,0)
dLDOY = DateSerial(Year(dNow) + 1, 1,0)
'Friday of this week
dLFOW = (dNow + (5 - Weekday(dNow, vbMonday)))
'Last Friday of Month
dLFOM = (dLDOM + (5 - Weekday(dLDOM, vbMonday)))
objEnv("Year") = Year(dNow)
objEnv("Month") = Right("0" & Month(dNow), 2)
objEnv("Day") = Right("0" & Day(dNow), 2)
objEnv("Hour") = Right("0" & Hour(dNow), 2)
objEnv("Minute") = Right("0" & Minute(dNow), 2)
objEnv("Second") = Right("0" & Second(dNow), 2)
'Day of Week
objEnv("DOW") = Weekday(dNow, vbMonday)
'Day of Week Name
objEnv("DOWN") = WeekDayName(WeekDay(dNow), 1)
'Week of Month
objEnv("WOM") = -Int(-((WEEKDAY(dNow-DAY(dNow)+1, 2) + DAY(dNow) -1) / 7))
'Last Day of Month
objEnv("LDOM") = Right("0" & Day(dLDOM), 2)
'Last Day of Year
objEnv("LDOY") = Right("0" & Day(dLDOY), 2)
'Last Friday of Week
objEnv("LFOW") = Right("0" & Day(dLFOW), 2)
'Last Friday of Month
objEnv("LFOM") = Right("0" & Day(dLFOM), 2)
sCommand = "%COMSPEC% /C "
WshShell.Run(sCommand + BatchFile)
WScript.Quit
'************************
'
' Display Help Message
'
'************************
Sub DisplayHelpMessage()
Dim sHelpMessage
sHelpMessage = "Usage:" & vbCrLf
sHelpMessage = sHelpMessage & Wscript.FullName & " RunNow <CommandFile|BatchFile>" & vbCrLf
sHelpMessage = sHelpMessage & "Optionally use environment variables in file: %Year% %Month% %Day% %Hour% %Minute% %Second%" & vbCrLf
sHelpMessage = sHelpMessage & " %DOW% %DOWN% %WOM% %LDOM% %LDOY% %LFOW% %LFOM%" & vbCrLf
WScript.Echo sHelpMessage
End Sub
UsageAs you can see in the source Help Message, it is sufficient to use the environment variables %Year% %Month% %Day% %Hour% %Minute% or %Second% in your batch files. To give an example: You could make a command file called mydir.cmd with the following contentsdir > MyLog%Year%%Month%%Day%-%Hour%%Minute%%Second%.log
Instead of running this file directly, run via the VB Script: RunNow.vbs mydir.cmd
There is also now some extra environment variables like Last Day of Month, Week of Month etc.. Thats it. I hope this will be usefull to you. Peter Verijke
|
||||||||||||||||||||||