0

I want to create a folder that uses TIME formatted in a specific way. I want the format to be in hh.mm.ss since you can't use a : in a folder name.

When I use set CurTime=%time:~0,2%.%time:~3,2%.%time:~6,2%, I get an output with a leading space before 10:00 AM and five trailing spaces as well (not sure why). I can remove the spaces by adding the line set CurTime=%CurTime: =%, but I want to add a leading zero if the time is earlier than 10:00 AM.

How can I do this?

Edit for clarification: I have the time formatted the way I want it, but I want to replace the leading space with a 0 if the hh portion is less than 10. This is not a duplicate question of How to get current datetime on Windows command line, in a suitable format for using in a filename?.

Community
  • 1
  • 1
indy-pc
  • 27
  • 1
  • 6
  • 2
    It would help if you were to provide your `Echo=%TIME%` output and the expected and actual outputs when running your command. – Compo Dec 07 '16 at 14:23
  • The command `set "CurTime=%TIME::=.%"` would replace `:` by `.`, by the way... – aschipfl Dec 07 '16 at 14:25
  • `echo %time%` would give a result in the format of hh:mm:ss.dd where hh could contain a leading space and dd is the milliseconds. I don't want milliseconds at all and I want to make sure the end result would have a leading zero instead of a space. – indy-pc Dec 07 '16 at 14:32
  • 2
    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 Dec 07 '16 at 14:39

3 Answers3

2
set "mytime=%time:~0,-8%"
set "mytime=%mytime: =0%"
set "mytime=%mytime::=.%"
echo "%mytime%"

should show the result you want. The first line removes the last 8 characters from time (which should be the 5 spaces + .dd) and the other two make the substitution of unwanted characters.

The reason for the 5 trailing spaces may be that some time formats allow " a.m." and the easy way is to simply replace the unwanted parts with spaces.

Magoo
  • 77,302
  • 8
  • 62
  • 84
1

If you have read this ==> Windows batch file redirect output to logfile with date/time

You can be able to do like this one :

@echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set dt=%%a
set datestamp=%dt:~0,8%
set timestamp=%dt:~8,6%
set YYYY=%dt:~0,4%
set MM=%dt:~4,2%
set DD=%dt:~6,2%
set HH=%dt:~8,2%
set Min=%dt:~10,2%
set Sec=%dt:~12,2%
set stamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%
echo stamp: "%stamp%"
pause
echo datestamp: "%datestamp%"
pause
echo timestamp: "%timestamp%"
pause
set MyVar=%HH%.%Min%.%Sec%
echo My desired Variable in this format hh.mm.ss to use is : %MyVar%
pause
mkdir "c:\%MyVar%"
pause
Community
  • 1
  • 1
Hackoo
  • 18,337
  • 3
  • 40
  • 70
  • Breaking apart the time into individual HH, Min and Sec components worked. By adding a `set HH=%HH: =0%` line, I could make sure that if there was a space in the hour, it would be replaced with a 0. Thanks! – indy-pc Dec 09 '16 at 13:04
  • wmic is obsolette – user2956477 Jun 26 '23 at 04:55
0

There are many ways to achieve your goal.

Here's a crazy untested one:

@ECHO OFF
SET "CurTime="
FOR /F "TOKENS=2-4 DELIMS=: " %%A IN ('ROBOCOPY/NJH /L "\|" NULL'
) DO IF NOT DEFINED CurTime SET "CurTime=%%A.%%B.%%C"
ECHO(%CurTime%
TIMEOUT -1
Compo
  • 36,585
  • 5
  • 27
  • 39