batch: time to var

December 18, 2010 at 18:27:36
Specs: Windows 7
For /f "tokens=2-3 delims=: " %%a in ('time /t') do (set time2=%%a-%%b)
this is what i do to obtain the time in hh-mm for naming a file
but i also need to get the seconds in time ie: hh-mm-ss
is there a way to do this?

See More: batch: time to var

Report •

#1
December 19, 2010 at 01:00:53
[1] forget DOS

[2] If the var %time% looks much like this:

15:54:41.03

try this
==============================================
@echo off & setLocal EnableDELAYedeXpansion

for /f "tokens=* delims= " %%a in ("%TIME%") do (
set T=%%a
set T=!T::=-!
set T=!T:~0,8!
)
echo.!T!


=====================================
Life is too important to be taken seriously.

M2


Report •

#2
December 19, 2010 at 08:04:01
M2, nice solution, but why do you need a FOR loop? Using your idea, here's a simpler version:

set t=%time:~0,8%
set t=%t::=-%

I wish I could do that in one line, but can't figure out how.


Report •

#3
December 19, 2010 at 19:10:57
hello Klint & M2!
On my old funky clunker, time /t only yields the hours and minutes, and i'm too
dumb/lazy to go into registry and fix the time format, which is probably the source
of the problem, so i did this:
for /f "tokens=5-7 delims=:. " %%a in ('echo. ^|time') do set s=%%a-%%b-%%c

(time without /t gives the seconds and hundredths)
you could add a "skip=1" if you want to be anal about it, but why bother?


Report •

Related Solutions

#4
December 19, 2010 at 23:24:34
Hi nbrane,

Thanks, that is indeed one line. But there's no need to spawn another process to do echo.|time, since you can use the variable %time%:

for /f "tokens=1-3 delims=:." %%a in ("%time%") do set s=%%a-%%b-%%c

Not sure if this is less or more complicated than two simple commands.


Report •

#5
December 20, 2010 at 01:39:03
Hi gang,

The only excuse for FOR loop is that I've become a bit of a one-trick pony in my old age.

Like DATE substrings, TIME substrings will fail whenevr used on a box with a different layout.

Some day I need to get hot and do the debug code to get time from BIOS. But not today. Too lazy.


=====================================
Life is too important to be taken seriously.

M2


Report •

#6
December 20, 2010 at 03:26:50
And here I was, afraid you got yourself a 64-bit OS.

How To Ask Questions The Smart Way


Report •

#7
December 20, 2010 at 14:25:52
Thank you!

Report •

Ask Question