
#DOS BATCH FILE TIME LEADING ZEROS WINDOWS#
However, above script failed to generate file if hour is less than 10 as it returns ‘ 8’ for example with leading space and Windows 2003 rejects file name with space. Set timestamp=%year%%month%%day%%hour%%minute%%seconds% We suffixed data file name with timestamp to make it unique as below As a POC and to keep costs down, we decided to use windows batch file scripting to do this. This makes for a more reliable method of making folders and file names using the %date% and %time% variables.Recently, we had requirement to generate unique file name to transfer data to another DC in an encrypted format from Windows 2003 server. So at 2 am you end up with 102, take the last two characters and you get 02.
#DOS BATCH FILE TIME LEADING ZEROS CODE#
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. The %time~-11,2% would return a 2 with a space before it breaking the whole thing. The problem is the hour part of the %time% variable doesn’t return a two digit hour like at 2 am. 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. This returns: 20110211_191655 and was exactly what I wanted. 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. This takes the %date% variable starts at the first character after the 10th and grabs 4 characters.Ĭool, 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.) I found that you can take just a part of the %date% variable by doing the following: %date~10,4% 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. Since I’m using this to create file and folder names you can’t have slashes and colons etc. The %Date% variable will give you “Fri ”. The %Time% variable in a batch file will give you a time like 18:55:30.32 I’ll explain why it looks more complicated than you (and I) might have thought it should be afterwards.

bathints.zip: 42013: A kind of a batch user's primer, some useful hints: batkit58.zip: 90041: BATKIT v5.8 - Batch file utilities. The first part is the year then month then date followed by an underscore the hour then minutes then seconds.įor the impatient out there here is the final code I use to get this date and time stamp: MS-DOS batch files debugger program, Armin Mueller: batfaq23.zip: 14861: FidoNet Echo BATPOWER Frequently Asked Questions, r.0203 in an ASCII text format. What I wanted to end up with is something like 20110211_185530.zip. 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 20110211185530.zip. I’ve lately been using batch files more and more to do backups and other things. I’ve lately been using batch files more and more to do backups and other things.
