Friday, March 13, 2015

HOWTO Batch Language Date and Time

Overview

Batch language is super basic but there is one trick BATCH supports for getting the date and time. Looking through many different blogs I was able to assemble this HOWTO.

I use this syntax for a little script that I wrote that to backup/archive various coding directories I have on my workstation. I use Windows Backups but I find I trust a simple BAT file that creates date/time stamped 7z files. I wish Windows had a “time machine” like facility but use what you have vs fuss.
To create a proper date/time stamp you can use the following technique.

Syntax/Technique

If you include something like this in your Windows BAT file, you will get a date and time that can be applied to a filename or similar.
set hr=%time:~0,2%
if "%hr:~0,1%" equ " " set hr=0%hr:~1,1%
set FNAME=ARC_%date:~-4,4%%date:~-10,2%%date:~-7,2%_%hr%%time:~3,2%%time:~6,2%.zip
echo %FNAME%
… you will get a result like this
C:\Users\k.langer>echo %FNAME%
ARC_20150209_115340.zip

Conclusion

You can use this trick when automating backups or similar.
[For More Information http://stackoverflow.com/questions/7727114/batch-command-date-and-time-in-file-name]

No comments: