Introduction
Have you ever tried to use date and time in logfiles or batch/command files?
This is not easy without special software. For me, this was a problem because I needed a separate logfile every time an event happened. I also needed this to create backup schedules. The possibilities of batch command are too limited, and different language OS will react differently, so it is hard to roll such batch files out in a multi language OS environment.
Solution
The solution is very simple. I run a VB script where it is easy to get date time and other parameters. These values are stored in environment variables which can be used in batch files.
Because these environment variables are only valid in that particular shell, we need to call the batch file within the VB Script.
Copy the following code in a file called RunNow.cmd:
Dim dDay, dLDOM, dLDOY, dLFOW, dLFOM
Dim ArgObj
Dim BatchFile
Dim sCommand
Dim WshShell Dim objEnv Dim sGetMyVar
Set ArgObj = WScript.Arguments
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)
dLFOW = (dNow + (5 - Weekday(dNow, vbMonday)))
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)
objEnv("DOW") = Weekday(dNow, vbMonday)
objEnv("DOWN") = WeekDayName(WeekDay(dNow), 1)
objEnv("WOM") = -Int(-((WEEKDAY(dNow-DAY(dNow)+1, 2) + DAY(dNow) -1) / 7))
objEnv("LDOM") = Right("0" & Day(dLDOM), 2)
objEnv("LDOY") = Right("0" & Day(dLDOY), 2)
objEnv("LFOW") = Right("0" & Day(dLFOW), 2)
objEnv("LFOM") = Right("0" & Day(dLFOM), 2)
sCommand = "%COMSPEC% /C "
WshShell.Run(sCommand + BatchFile)
WScript.Quit
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
Usage
As 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 contents:
dir > MyLog%Year%%Month%%Day%-%Hour%%Minute%%Second%.log
Instead of running this file directly, run via the VB Script:
RunNow.vbs mydir.cmd
Now, there are also some extra environment variables like Last Day of Month, Week of Month etc. These variables can also be used for naming or comparing to take other actions in batch files. I use it to create Day, Week and Month Backups for some data.
That's it.
I hope this will be useful to you.