Log file with timestamp in Windows

How to build the log file name with timestamp in the format Log_yyyy_MM_dd_HHmm.log.

Batch file

@echo off

REM get date in format yyyy-MM-dd
FOR /f "tokens=1-3 delims=- " %%a IN ("%DATE%") DO (SET filedate=%%a_%%b_%%c)

REM get time in format HHmm
FOR /f "tokens=1-2 delims=:" %%a IN ("%TIME%") DO (SET filetime=%%a%%b)

REM if time in format Hmm - add leading zero
IF "%filetime:~0,1%"==" " SET filetime=0%filetime:~1,3%

REM prepare log file name in format Log_yyyy_MM_dd_HHmm.log
SET logfilename=C:\Logging\Log_%filedate%_%filetime%.log

REM do something redirecting result to %logfilename%

ECHO See the logfile: %logfilename%

Adjusting date pattern to correspond server settings

The above script assumes the server yyyy-MM-dd as the date format and Hmm as the time format (H stands for 24 hours time format). It uses command line tool DATE to get the current date and system environment variable %TIME% to get the current time. So the easiest way to make it work is to adjust server date and time short format to yyyy-MM-dd and Hmm in Region and Language settings. But this might be not the best solution, the other option is to adjust the script.

Let’s consider the line:

FOR /F "tokens=1-3 delims=- " %%a IN ('date /t') DO (SET filedate=%%a_%%b_%%c)

This line gets the date, parses it assuming format yyyy-MM-dd and builds the variable filedate in format yyyy_MM_dd.

FOR /F ["options"] %%variable IN (file-set) DO command [command-parameters]

This command reads and processes line by line all files from the files-set. You can read description of the command running FOR /?. In our case we get one line result of the command date /t and parse it using the specified options tokens and delims.

When option tokens is specified, additional variables are allocated. In our case we use first to third tokens, first token is assigned to variable a, second token is assigned to variable b, and third is assigned to variable c. We use these variables to set the variable filedate.

The option delims specifies the set of delimiters. In our case: space and minus signs.

So if your date format is ddd MM/dd/yyyy (e.g. Sat 09/10/2011), the command should be changed to

FOR /F "tokens=2-3 delims=/ " %%a IN ('date /t') DO (SET filedate=%%c_%%a_%%b)

The following line adds leading zero if necessary.

IF "%filetime:~0,1%"==" " SET filetime=0%filetime:~1,3%

Some other notes about batch files

DO part can be split over lines:

for /f "tokens=1-3 delims=- " %%a in ('date /t') do (
set year=%%a
set month=%%b
set day=%%c
)
set filedate =%year%_%month%_%day%

Or it can be written on one line with ampersand separator:

... do (set year=%%a&set month=%%b&set day=%%c)

To get last 3 characters you can use %var:-3%. To replace substring str1 with str2, you can use %var:str1=str2%.

Note that trailing space can be a part of variable. For example,

@echo off
set var =some text
echo "%var%" "%var %"
pause

This batch will print. %var% is not defined and thus empty.

"" "some text"

Further information

You can read more information about the batch commands in the help: FOR /? and SET /?.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s