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:
dosomething.cmd: @echo off setlocal set LOGFILE=%TEMP%dosomething.log :start 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%" :cleanup endlocalThis 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 somethingSplitting off the fractional seconds is left as an exercise for the reader ;o)
Code: @if .%ECHO%. == .. echo off rem // 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
Join the conversationComment
Share
Comments
Results
Contribute to the conversation