4

I'm running a Batch script on a German Win7 environment with the following variable:

%date%_%time:~0,2%-%time:~3,2%-%time:~6,2%

This script works quite well from 10:00 until 23:59. Between 0:00 and 9:59 i get a Syntax error (I suppose because the time has only one digit before the ":")

Can someone help me with this?

Thx!

SimonS
  • 1,387
  • 19
  • 44
  • check this : http://stackoverflow.com/a/19799236/388389 – npocmaka Jul 06 '15 at 08:34
  • Thank you! I ended up using this: `set hour=%time:~0,2% if "%hour:~0,1%" == " " set hour=0%hour:~1,1% set min=%time:~3,2% if "%min:~0,1%" == " " set min=0%min:~1,1% set secs=%time:~6,2% if "%secs:~0,1%" == " " set secs=0%secs:~1,1%` – SimonS Jul 06 '15 at 09:10

1 Answers1

2

Still locale dependent:

set "_datetime=%date%_%time:~0,2%-%time:~3,2%-%time:~6,2%"
set "_datetime=%_datetime: =0%"

For locale independent solution (and yes, without _ and - by then):

for /F "tokens=2 delims==" %%G in (
    'wmic OS get LocalDateTime /value'
) do @for /F "tokens=*" %%x in ("%%G") do (
    set "_datetime=%%~x"
)
set "_datetime=%_datetime:~0,14%"
goto :eof

Here the for loops are

  • %%G to retrieve the LocalDateTime value;
  • %%x to remove the ending carriage return in the value returned (wmic behaviour: each output line ends with 0x0D0D0A instead of common 0x0D0A).

See Dave Benham's WMIC and FOR /F: A fix for the trailing <CR> problem

JosefZ
  • 22,747
  • 4
  • 38
  • 62