Click here to Skip to main content
6,631,889 members and growing! (21,080 online)
Email Password   helpLost your password?
Languages » VBScript » General     Intermediate License: The Code Project Open License (CPOL)

Use date & time in batch/log files

By Peter Verijke

Use date and time in batch/log files
VBScriptWin2K, WinXP, Win2003, Dev
Posted:29 Sep 2003
Updated:1 Feb 2008
Views:122,498
Bookmarked:11 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
6 votes for this article.
Popularity: 2.00 Rating: 2.57 out of 5
1 vote, 16.7%
1
2 votes, 33.3%
2
1 vote, 16.7%
3
1 vote, 16.7%
4
1 vote, 16.7%
5

Introduction

Did 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 when an event happened.
I also needed this to create backup schedules.
The possibilities of batch command is to limited, and different language OS will react different, so it is hard to roll such batch files out in a multi language OS environment.

Solution

Te solution is very simpel.
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

 
'***********************************************
'
' 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


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 

There is also now 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 for some data Day, Week and Month Backups.

Thats it.

I hope this will be usefull to you.

Peter Verijke

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Peter Verijke


Member
I´m a freelance ICT consultant and Technical Project Manager for the last 15 years already.
Before that, I was a Software Developer for the Pharmaceutical and Petrochemical industry.
I developed a very wide knowledge in the ICT world due to my intrest.
This includes Networking (Lan, Wan), Routing Switching Bridging, etc...
Windows environment, which has no secrets for me.
Developing: To many languages to mension here.
Of course, the latest one on the list is dot net.

Occupation: Founder
Company: Computech bvba
Location: Belgium Belgium

Other popular VBScript articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 15 of 15 (Total in Forum: 15) (Refresh)FirstPrevNext
GeneralJust made use of this PinmemberOffbeatmammal11:30 31 Dec '08  
Generalhow can i use this? Pinmemberblajarbot21:26 4 Jan '07  
GeneralAM or PM PinsussAnonymous17:03 22 Jan '05  
GeneralRe: AM or PM PinsussAnonymous17:39 22 Jan '05  
GeneralRe: AM or PM Pinmember____0117:44 13 Jan '07  
GeneralCan be done with batch commands alone PinmemberPaul Bartlett0:27 18 Dec '03  
GeneralRe: Can be done with batch commands alone PinmemberRabidCow10:11 18 Dec '03  
GeneralRe: Can be done with batch commands alone Pinmemberfunny thing10:54 18 Dec '03  
GeneralRe: Can be done with batch commands alone PinmemberPeter Verijke0:13 19 Dec '03  
GeneralRe: Can be done with batch commands alone PinsussAnonymous1:36 25 Mar '05  
GeneralRe: Can be done with batch commands alone PinsussAnonymous9:45 8 Aug '05  
GeneralRe: Can be done with batch commands alone PinmemberPeter Verijke23:32 18 Dec '03  
GeneralRe: Can be done with batch commands alone PinmemberPaul Bartlett1:30 19 Dec '03  
GeneralRe: Can be done with batch commands alone Pinmemberaaac0073:32 24 Dec '03  
GeneralRe: Can be done with batch commands alone PinsussAnonymous20:01 9 Mar '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 1 Feb 2008
Editor:
Copyright 2003 by Peter Verijke
Everything else Copyright © CodeProject, 1999-2009
Web09 | Advertise on the Code Project