Wednesday, June 27, 2007

Printing Date and Time

What's this, you say?  We're getting down to such dumb topics after only a few posts?  Well, it might be dumb, but it's kind of useful.  Say you want to print the date and the time.  You would use the date and time command, obviously.  And they have the /t switch to allow no interaction which is nice for batch files.

date /t

time /t

Excellent.  Marvelous.  Jolly good.  But what if you want to put both of them on the same line?  Like if you want to create a log trace where you put date and time at the beginning of each line?  Well, the date and time commands won't really work well because they will put the date and time on different lines.  Luckily there is an alternative.

echo %DATE% %TIME% - Log message goes here.

This will print out the date, time and log message on one line.  Ideal for logging traces.

No comments: