I’ve lately been using batch files more and more to do backups and other things.
One of the things that’s given me troubles is using dates and times to add to folders or file names to help with organizing.
What I wanted to end up with is something like 20110211_185530.zip. The first part is the year then month then date followed by an underscore the hour then minutes then seconds.
For the impatient out there here is the final code I use to get this date and time stamp:
I’ll explain why it looks more complicated than you (and I) might have thought it should be afterwards.
set hh=%time:~-11,2%
set /a hh=%hh%+100
set hh=%hh:~1%
Set dateseed=%date:~10,4%%date:~4,2%%date:~7,2%_%hh%%time:~3,2%%time:~6,2%
echo %dateseed%
The %Time% variable in a batch file will give you a time like 18:55:30.32
The %Date% variable will give you “Fri 02/11/2010”.
Since I’m using this to create file and folder names you can’t have slashes and colons etc. mixed in there.
So, being a VB programmer I started out thinking I could use something like the mid function to take the date and time apart and then put it back together in the string I wanted.
I found that you can take just a part of the %date% variable by doing the following: %date~10,4%
This takes the %date% variable starts at the first character after the 10th and grabs 4 characters.
Cool, so you end up %date~10,4% = 2011 (unless you go back in time to last year or forward to next year in which it will be something else and if you can do that then you probably aren’t reading this.)
You can add a negative sign before the first number and it will go from right to left instead of left to right when setting character position.
So with %date:~10,4%%date:~4,2%%date:~7,2% = 20110211 (because today is 02/11/2011).
Take the time and you get %time:~-11,2%%time:~3,2%%time:~6,2% = 191551 (because it’s 7:15 pm at 51 seconds)
Based on these then I came up with:
Set dateseed=%date:~10,4%%date:~4,2%%date:~7,2%_%time:~-11,2%%time:~3,2%%time:~6,2%
echo %dateseed%
This returns: 20110211_191655 and was exactly what I wanted. So, why is the final code I gave you above so much more complicated? I started using the above code and had problems when it went to make a folder and the time was like 2 am.
The problem is the hour part of the %time% variable doesn’t return a two digit hour like at 2 am. The %time~-11,2% would return a 2 with a space before it breaking the whole thing.
So, the first lines in the above code compensates for this by taking the hour and adding 100 to it and then pulling out the last 2 characters of what remains. So at 2 am you end up with 102, take the last two characters and you get 02. This makes for a more reliable method of making folders and file names using the %date% and %time% variables.
There might be a much better, shorter way to accomplish this but I couldn’t find it so here’s the final code:
(until someone smarter than me tells me how silly I am for doing it this way and gives me something better)
set hh=%time:~-11,2%
set /a hh=%hh%+100
set hh=%hh:~1%
Set dateseed=%date:~10,4%%date:~4,2%%date:~7,2%_%hh%%time:~3,2%%time:~6,2%
echo %dateseed%
this is the best way to get date and time in a batch fle. I haven’t seen a more elegant and compact way of doing this.
however i did havea one small question. Is there any way, the calculation for hour [hh] can be shortened further?
This is a little late, but I came across your blog entry when searching for a solution to this exact same problem, so I wanted to say thanks!
set hh=%time:~0,2%
set hh=%hh: =0%
set dateseed=%date:~10,4%%date:~4,2%%date:~7,2%_%hh%%time:~3,2%%time:~6,2%
echo %dateseed%
Thanks much appreciated an elegant solution saved me hours … thank you
thank you so much for sharing – there are so many other google results to get close, but no cigar… it appears to me the /a +100 approach to making it single digit time friendly is perfect… THANK YOU!
A little script I finally came across while doing my head in looking for an answer. You were 5th on the list of newly opened but un-read pages!!
FOR /F “TOKENS=1 DELIMS=:” %%A IN (‘TIME/T’) DO SET HH=%%A
“%date:~-2,4%%date:~-7,2%%date:~-10,2%-%HH%;%time:~3,2% exe files.txt”
Generates a file named:
130618-05;58 exe files.txt
yymmdd-hh;mm exe files.txt
Got to admit sticking that extra ‘0’ in took some finding!!
I found this:
http://stackoverflow.com/questions/1192476/windows-batch-script-format-date-and-time
I came across this problem today and solved it with:
SET LOGTIME=%TIME: =0%
It replaces spaces with 0s and basically zero-pads the hour.
Thank you! You saved me a lot of time.
What about this?
(1) Replace the ‘:’ in the system time with nothing.
(2) Replace the ‘.’ in the system time with nothing.
(1) set timetemp=%time::=%
(2) set timeStamp=%timetemp:.=%
RESULT: 12223423
Thanks! This was helpful.
This was very helpful – thanks!
One thought. Consider what happens in the following
set year=%date:~10,4%
set month=%date:~4,2%
// operating system could create a delay here…..
set day=%date:~7,2%
There’s a race condition, in that if the above is called around midnight its possible (and has happened to me!) to get the wrong day. E.g. if date is 31 Jan just before midnight, and set day is called just after midnight then we have month=1, day=1 instead of month=1 day=31 (or month=2, day=1). In theory doing everything on one line still poses the same problem as execution of any program can be paused by the operating system at time.
Solution is to do above calcs on fixed variable
e.g.
set d=%date%
set year=%d:~10,4%
set month=%d:~4,2%
// some delay
set day=%d:~7,2%
Simerally for time. However, there is still a possible race condition as same technique wont work for both data and time.
Thank you very much for this information and for leaving it up so long–it is still very useful and was just what I needed.