This email address is being protected from spambots. You need JavaScript enabled to view it.

Windows DOS batch programming - system date and time

If you need to rename/create file (backup) using date or time using DOS batch file, use variables %DATE% and %TIME%. Sometimes %DATE% variable returns short name of the day of week before actual date. (e.g. fri 11.06.2010.) To strip dayname part use substring: %DATE:~4,10% (from pos 4 takes 10 chars), so our date var will be now 10.06.2010. If you want to change dateformat then go to Control Panel/Regional Settings/Reginal options/Customize (Windows XP) and set desired date format.

My time format is HH:MM:SS,MS, because DOS doesn't not tolerate ":" and "," inside filename then it's needed to replace those char's. I've replaced them with "." and I don't neeed miliseconds, so result is: HH.MM.SS.

Command to rename file.txt with timestamp.

ren file.txt "file%date:~4,10%-%time:~0,2%.%time:~3,2%.%time:~6,2%.txt"

result: file10.06.2010-10.02.30.txt

 

I'm using bat script to periodically backup content of the svn (subversion) into txt, zip file with current date (backup is executed once a day), then remove oriignal txt file:

svnadmin dump d:\svnrep > e:\svn_bcp\svnbcp_%date:~4,10%.txt
zip -r svnbcp_%date:~4,10%.zip svnbcp_%date:~4,10%.txt
del svnbcp_%date:~4,10%.txt

 

Note: zip.exe is custom windows-console utility that i use for zipping.

 

References: