Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  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 log on before posting a comment.

If you don't have a username & password, please register now.




Top Viewed ArticlesView all articles
Friday at PASS Europe 2006

Kevin talks about the closing day of the event and shares a funny Microsoft film. ...

PsExec

This freeware utility lets you execute processes on a remote system and redirect output to the local system. ...

Escape From Yesterworld

Kevin points you to the funniest SQL Server website ever! ...


Windows OSs Whitepapers Why SaaS is the Right Solution for Log Management

Related Events 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 Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.


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 Windows Dev Pro IT Job Hound ITTV
IT Library Technology Resource Directory Connected Home Windows Excavator Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2008 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing