1

I am using the following simple line in my windows batch file to get the current time stamp to a String format so that I can use it later in the batch file to create a folder with same name.

set TIME_STAMP=%DATE:/=-%_%TIME::=-%

I observed that when the time is single digits, say 9:31 AM, I get the String like this:

08-10-2015_ 9.31.52.57

Notice the space between the characters _ and 9. When the system time is say 10:31 AM, it all works fine, like

08-10-2015_10.31.52.57

Is there something I can do to make the time stamp as

08-10-2015_09.31.52.57

when I have hours in single digits?

Ayusman
  • 7,715
  • 20
  • 72
  • 128
  • 1
    You could not have used that code to produce your result. You must have used `%TIME::=.%` – dbenham Oct 08 '15 at 04:17
  • 2
    You already used `%var:search=replace%` to search and replace. Hmm, I wonder how you could convert a space into a zero? – dbenham Oct 08 '15 at 04:18
  • 1
    Possible duplicate of [How to get current datetime on Windows command line, in a suitable format for using in a filename?](http://stackoverflow.com/questions/203090/how-to-get-current-datetime-on-windows-command-line-in-a-suitable-format-for-us) – Mofi Oct 08 '15 at 05:08
  • dbenham has already stated how with humour :) – Paul Oct 08 '15 at 05:09

2 Answers2

1

just do this

set TIME_STAMP=%DATE:/=-%_%TIME::=-%
echo %TIME_STAMP: =0%
Paul
  • 2,379
  • 2
  • 12
  • 27
0

Probably the simpliest approach:

set "TIME_STAMP=%DATE:/=-%_%TIME::=-%"
set "TIME_STAMP=%TIME_STAMP: =0%"

Result:

==> echo "%TIME_STAMP%"
"08.10.2015_07-42-08,18"

==>
JosefZ
  • 22,747
  • 4
  • 38
  • 62