Link to home
Start Free TrialLog in
Avatar of Simon336697
Simon336697Flag for Australia

asked on

Using the time /t command

for /f "tokens=1,2 delims=:" %%a in ('time /t') do @echo scripttime=%%a%%b

Output is:
scripttime=0439 PM

What id like is:
scripttime=04_39_23_pm

So, id like to do the following:
1) factor in seconds as well eg. the 23 above,
2) remove the space before the PM and replace it with an underscore
3) convert all letters ot lowercase


Any help greatly appreciated.
ASKER CERTIFIED SOLUTION
Avatar of beanrod
beanrod
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Simon336697

ASKER

Hi beanrod, thanks for that.

I extended yours to include whether the time was a single digit hour or second hour.
Thanks so much.


if "%TIME:~0,1%" == " " GOTO SINGLEDIGITHOUR
GOTO DOUBLEDIGITHOUR

:SINGLEDIGITHOUR
set Scripttime=0%TIME:~1,1%_%TIME:~3,2%_%TIME:~6,2%
GOTO END

:DOUBLEDIGITHOUR
set Scripttime=%TIME:~0,2%_%TIME:~3,2%_%TIME:~6,2%
GOTO END
Or another one:

@echo off

if defined Scripttime set Scripttime=

if "%TIME:~0,1%" == " " GOTO SINGLEDIGITHOUR
GOTO DOUBLEDIGITHOUR

:SINGLEDIGITHOUR
set Scripttime=0%TIME:~1,1%_%TIME:~3,2%_%TIME:~6,2%_AM
GOTO END

:DOUBLEDIGITHOUR
rem cater for 10AM or 11AM
if "%TIME:~0,2%" == "10" GOTO DOUBLEDIGITHOUR_10OR11AM
if "%TIME:~0,2%" == "11" GOTO DOUBLEDIGITHOUR_10OR11AM
GOTO DOUBLEDIGITHOUR_PM

:DOUBLEDIGITHOUR_10OR11AM
set Scripttime=%TIME:~0,2%_%TIME:~3,2%_%TIME:~6,2%_AM
GOTO END

:DOUBLEDIGITHOUR_PM
set Scripttime=%TIME:~0,2%_%TIME:~3,2%_%TIME:~6,2%_PM


:END
Excellent glad you could manipulate it to your usage