SETSTAMP.BAT – a generic script for “time stamping” file names

From time to time I’m asked how to add timestamp to the file name(s) with nothing more than a standard commands available on every Windows 2000/2003/XP box command line.
You’ll find tons of different recipes on the net, my favorite approach is to use generic script that we can call in our batch files. I attached one such generic batch script to this thread that I’m frequently using in my batch files. You’ll have to customize the script for your local environment, I live in a country with date format DD.MM.YYYY and HH:MI:SS and this is the expected format by the script (FOR statement).

Example of using generic script SETSTAMP.BAT in batch files:

:: How to add time stamp to the export log file name
:: export-scott.bat

:: by calling setstamp.bat we'll set the following variables:
:: %DDMMYY%, %DDMMYY-HHMMSS%, %DDMMYY-HHMMSSFF%.
call setstamp

:: let's timestamp our export dmp/log files...
exp scott/tiger@oracle file=scott-%ddmmyy%.dmp log=scott-%ddmmyy%.log

:: or use more specific version with time part as well....
:: another call to setstamp will refresh the time
call setstamp
exp scott/tiger@oracle file=scott-%ddmmyy-hhmmss%.dmp log=scott-%ddmmyy-hhmmss%

:: let's display the time the script ended...
:: another call to setstamp followed by echo...
call setstamp
echo %ddmmyy-hhmmss% 

SETSTAMP.BAT source code is here:

@echo off
:: Script: setstamp.bat
:: Author: Alesk
:: Generic script for composing timestamp string that you can use to form log file names.
:: Requirement: Windows 2000/XP/2003. Windows NT4 doesn't support date/time system variables,
:: so you'll need to customize the script with date & time commands to make this script work on NT4.
:: Also, you'll need to customize script for your local date format (check with: echo %date% and echo %time%). 
:: This script anticipate date format as: Wekday DD.MM.YYYY and for time HH:MM:SS,FF where FF is 1/100 of the second.
:: 
:: 
:: Example (test.bat):
:: call setstamp
:: echo Hello!! > mylog%ddmmyy%.log
:: ...
:: call setstamp 
:: echo This is second log ... > my2log%ddmmyy-hhmmss%.log

:: Let's go...
:: Get and parse date (in Windows 2000 and later you can use system variables %date% and %time% instead of
:: commands date and time)
:: Customize delims parameter in FOR statement if your date format is delimited in a different way.
FOR /f "tokens=1-4 delims=. " %%i in ("%date%") do (
	set dayofweek=%%i
	set day=%%j
	set month=%%k
	set year=%%l)

:: Get and parse system time
:: Customize delims parameter in FOR statement if your time format is delimited in a different way.
FOR /f "tokens=1-4 delims=:," %%i in ("%time%") do (
	set hour=%%i
	set minute=%%j
	set second=%%k
	set fraction=%%l)

:: From this point onward you are free to format system variables with basic elements such as day, month, hour... 
:: I prefer abbreviation that I can easily remember, here are my three favorite variables:

set ddmmyy=%day%%month%%year%
set ddmmyy-hhmmss=%day%%month%%year%-%hour%%minute%%second%
set ddmmyy-hhmmss-ff=%day%%month%%year%-%hour%%minute%%second%-%fraction%

:: Test section
:: echo %ddmmyy%
:: echo %ddmmyy-hhmmss%
:: echo %ddmmyy-hhmmss-ff%

:: END

Posted on 20.02.2008, in Scripting and tagged . Bookmark the permalink. Comments Off on SETSTAMP.BAT – a generic script for “time stamping” file names.

Comments are closed.