Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 


September 2003

An Easier Way to Name Log Files with the Current Date and Time

RSS
Subscribe to Windows IT Pro | See More Windows NT 4.0 Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

Download the Code Here

Reader to Reader: "Automatically Name Log Files with the Current Date and Time" (February 2002, http://www.winnetmag.com, InstantDoc ID 23468) uses the Ntdate command, which is in Jeffery Harris's ANITX Toolset, to automatically name log files. However, Windows NT's Date and Time commands are more than adequate for this task.

I wrote the batch file that Listing 1 shows to create date- and time-specific logs. This batch file, which requires NT 4.0 or later with command-line extensions, sets several variables, including date (yyyymmdd), time of day (hhmmss), and timestamp (yyyymm-ddhhmmss-nn, where nn is hundredths of seconds). To timestamp records, you can run the code that Listing 2 shows to create a daily log file (yyyymmdd.log) and stamp each record with the time of day to 1-second accuracy (hhmmss). If you need multiple log files for the same day, you can replace the first three lines with

call GetTimeStamp mydate
echo %timestamp% log file 
%mydate%.log created. > 
%mydate%.log

to create a log file (yyyymmdd-hhmmss-nn.log) with date and timestamps to within 1/100 second.

To work on NT 4.0, GetTimeStamp.bat needs to trick the Time command into returning through the command

time <nul >~datetime.txt

Then, the batch file parses the first line of the output file. (The first For/f command could instead use Date/T, but the second can't use Time/T because Time/T displays time in a 12-hour clock format.)

Windows 2000 offers a more elegant solution. GetTimeStamp.bat can dispense with writing and reading ~datetime.txt and simply use the %time% and %date% environment variables. The For/f commands become

for /f "tokens=1-4 delims=/
 " %%i in ("%date%")
for /f "tokens=1-4 delims=.: 
" %%i in ("%time%")

End of Article



Reader Comments
Nice script ... I was looking to do something very similar, and you have got it all figured out already. Thanks.

delawhere December 14, 2004 (Article Rating: )


Windows 2000 solution doesn't pad early hours with zeroes

Anonymous User January 26, 2005 (Article Rating: )


Windows 2K offers a much more elegant solution by using variable substitution: just try this one:

echo %date:/=%




Anonymous User June 01, 2005 (Article Rating: )


You must be a registered user or online subscriber to comment on this article. Please log on before posting a comment. Are you a new visitor? Register now




Top Viewed ArticlesView all articles
WinInfo Short Takes: Week of November 23, 2009

An often irreverent look at some of the week's other news, including some post-PDC some soul searching, a Google Chrome OS announcement and a Microsoft response, Windows 7 off to a supposedly strong start, the Jonas Brothers and Xbox 360, and so much more ...

2009 Windows IT Pro Editors' Best and Community Choice Awards

Picking a favorite product from an impressive crowd of competitive offerings is never an easy task, and such was the case with our Editors' Best and Community Choice awards this year. ...

Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...


Related Events Deep Dive into Windows Server 2008 R2 presented by John Savill

The Easiest Way to Save Time and Money on E-mail and SharePoint Management

Windows, Unix, Linux Interoperability

Check out our list of Free Email Newsletters!

Windows OSs eBooks Understanding and Leveraging Code Signing Technologies

A Guide to Windows Certification and Public Keys

SQL Server Administration for Oracle DBAs

Related Windows OSs Resources Introducing Left-Brain.com, the online IT bookstore
Looking for books, CDs, toolkits, eBooks? Prime your mind at Left-Brain.com

Discover Windows IT Pro eLearning Series!
Clear & detailed technical information and helpful how-to's, all in our trademark no-nonsense format


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro DevProConnections IT Job Hound
Left-Brain.com Technology Resource Directory asp.netPRO ITTV Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2009 Penton Media, Inc. Terms of Use | Privacy Statement