Sunday 20 July 2008

Batch programming tip#12: Creating a timestamp

Now we can substring, creating a timestamp is really easy.

This is one simple way of doing this using the %date% and %time% system variables to create timestamped files.


@echo off

:create_file
call :get_timestamp
type NUL > %timestamp%.file
goto eof

:get_timestamp
set d_stamp=%date:~11%%date:~8,2%%date:~5,2%
set t_stamp=%time:~0,2%%time:~3,2%%time:~6,2%%time:~9%
set timestamp=%d_stamp%%t_stamp%
goto blackhole

:eof
echo Press any key to quit...
pause > NUL
goto blackhole

:blackhole

(We reverse year, month and day so that the files are naturally sorted in the 'right' order.)

The create_file section retrieves the timestamp (e.g. 2008071719410698) created by the get_timestamp subroutine and uses it to create an empty file. Obviously this can have multiple uses like timestamping log information for instance.

(Note: day and month probably appear in a different order according to sytem locale.)

Thoughts?

No comments:

Online Marketing
Add blog to our blog directory blog search directory Blog Directory Blogarama - The Blog Directory