Script to improve date and time logging

This tip was submitted to the searchWin2000 Tip Exchange by member Bob Daniel. Let other users know how useful it is by rating the tip below.

Many ad hoc scripts benefit from logging events with date and time stamping, but Windows provides no flexible way to control inserting the date and time into log files. This script parses the output of 'date' and 'time' and inserts the various values into environment variables that begin with DT_, making it easy to call from within another script for logging.

The script sets end of month (%DT_EOM%) and leap year (%DT_LEAP%) as binary values (0 is no, 1 is yes). It was originally written under NT4 to run NTBACKUP under at. It works on Win2000 and should work on XP/.NET.

Sample use:


@echo off
set LOGFILE=%TEMP%dosomething.log
  call getdt
  echo %DT_YEAR%:%DT_MONTH%:%DT_DOM%:%DT_HOUR%:%DT_MIN%:%DT_SEC% started
doing something > "%LOGFILE%"
  rem // do stuff
  call getdt 
  echo %DT_YEAR%:%DT_MONTH%:%DT_DOM%:%DT_HOUR%:%DT_MIN%:%DT_SEC% finished
doing something > "%LOGFILE%"
This script produces a log file, starting each line with easily sortable date and time, example:
2002:03:05:01:05:03.46 started doing something
2002:03:05:03:40:58.12 finished doing something
Splitting off the fractional seconds is left as an exercise for the reader ;o)
		Code: @if .%ECHO%. == .. echo off
	rem //

Requires Free Membership to View

// GETDT.CMD rem // exports output of date and time into environment variables rem // variable list created by this script for %%a in ( DT_MIN DT_HOUR DT_SEC DT_MONTH DT_YEAR DT_DOW DT_DOM DT_EOM DT_LEAP ) do set %%a= rem // rem // split the output of 'date' rem // sample output: rem // The current date is: Sat 08/12/2000 for /f "tokens=5-6" %%a in ('echo.^|date^|find "current"') do ( set DT_DOW=%%a for /f "tokens=1-3 delims=/" %%c in ('echo %%b') do ( set DT_MONTH=%%c set DT_DOM=%%d set DT_YEAR=%%e ) ) rem // rem // because 'date' and 'time' are parsed rem // in separate operations, rem // times within sub-seconds of midnight rem // could be recorded on the previous day rem // rem // rem // split the output of 'time' rem // sample output: rem // The current time is: 15:44:10.03 for /f "tokens=5" %%a in ('echo.^|time^|find "current"') do ( for /f "tokens=1-3 delims=:" %%c in ('echo %%a') do ( set DT_HOUR=%%c set DT_MIN=%%d set DT_SEC=%%e ) ) rem // rem // calculate leap year rem // DT_LEAP=1 means we're in a leap year rem // DT_LEAP=0 means not in a leap year :leap set /a DT_LEAP=%DT_YEAR% %% 400 if "%DT_LEAP%" == "0" ( set DT_LEAP=1 goto :eom ) set /a DT_LEAP=%DT_YEAR% %% 100 if "%DT_LEAP%" == "0" ( set DT_LEAP=0 goto :eom ) set /a DT_LEAP=%DT_YEAR% %% 4 if "%DT_LEAP%" == "0" ( set DT_LEAP=1 goto :eom ) else ( set DT_LEAP=0 ) :eom set DT_EOM=0 if "%DT_DOM%" == "28" if "%DT_MONTH%" == "02" if "%DT_LEAP%" == "0" set DT_EOM=1 if "%DT_DOM%" == "29" if "%DT_MONTH%" == "02" set DT_EOM=1 if "%DT_DOM%" == "30" if "%DT_MONTH%" == "04" set DT_EOM=1 if "%DT_DOM%" == "30" if "%DT_MONTH%" == "06" set DT_EOM=1 if "%DT_DOM%" == "30" if "%DT_MONTH%" == "09" set DT_EOM=1 if "%DT_DOM%" == "30" if "%DT_MONTH%" == "11" set DT_EOM=1 if "%DT_DOM%" == "31" set DT_EOM=1

This was first published in March 2002

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.