Nerd-cen-tric, adj. 1. Holding the view that technology is the center, object, and norm of all experience.

Date / Time Stamping Files

One common tool I need in scripts is the ability to time/date stamp a file.  The example below shows a simple method for adding time and/or date information to the end of a log file:

@echo off
Echo [-] Echo [+] Set Date/Time Variables
Echo [-] Echo.
Set day=%DATE:~0,3%
Set mm=%DATE:~4,2%
Set dd=%DATE:~7,2%
Set yyyy=%DATE:~10,4%
Set hour=%time:~0,2%
if “%time:~0,1%”==” ” set hour=0%hour:~1,1%
Set min=%time:~3,2%
Set sec=%time:~6,2%
echo Here’s a sample date stamped file: logfilename-%yyyy%-%mm%-%dd%.log
echo Here’s a sample time stamped file: logfilename-%hour%%min%%sec%.log

One cool thing about this method is that it checks for times before 10am.  If the hour is not two digits a zero is added.  This helps with sorting as all of your filenames will be the same length/format.

Leave a Reply

You must be logged in to post a comment.

Nerdcentric Twitter Feed