Discussion:
Writing a basic script to show time in format hhmmss
(too old to reply)
chanchoth
2005-07-25 06:01:14 UTC
Permalink
Hi batch guru,

I would like to create a simple batch file which can show the time in
this format hhmmss. My script is as follow:

showtime.bat
============
echo %time:~0,2%%time:~3,2%%time:~6,2%

The problem is when the time is 1-9, it shows only hmmss. All I want is
when the time is from 1-9, I would like it to have 0 infront of the
time so that I can have this format hhmmss.

Thanks for your kind assistance.
foxidrive
2005-07-25 06:21:37 UTC
Permalink
Post by chanchoth
Hi batch guru,
I would like to create a simple batch file which can show the time in
showtime.bat
============
echo %time:~0,2%%time:~3,2%%time:~6,2%
The problem is when the time is 1-9, it shows only hmmss. All I want is
when the time is from 1-9, I would like it to have 0 infront of the
time so that I can have this format hhmmss.
Thanks for your kind assistance.
This is untested.

if [%time:~0,1%]==[ ] (echo 0%time:~1,1%%time:~3,2%%time:~6,2%
) else (
echo %time:~0,2%%time:~3,2%%time:~6,2%
)
chanchoth
2005-07-25 07:59:14 UTC
Permalink
Thank you so much for your prompt response and it is very useful. Could
I get something like computername_mmddyyyy_hhmmss.txt and past it to
the variable (var)? I think that my script below is not correct, could
you please assist me?

if [%time:~0,1%]==[ ] (
$var=%computername%_%date:~4,2%%date:~7,2%%date:~10,4%0%time:~1,1%%time:~3,2%%time:~­6,2%.txt
) else (
$var=c:\%computername%_%date:~4,2%%date:~7,2%%date:~10,4%%time:~0,2%%time:~3,2%%time:~6­,2%.txt

)
echo $var

Many thanks for your assistance.
foxidrive
2005-07-25 08:32:11 UTC
Permalink
Post by chanchoth
Thank you so much for your prompt response and it is very useful. Could
I get something like computername_mmddyyyy_hhmmss.txt and past it to
the variable (var)? I think that my script below is not correct, could
you please assist me?
@echo off
set tm=%time:~0,2%%time:~3,2%%time:~6,2%
if [%tm:~0,1%]==[ ] (set tm=%tm: =0%)
set dt=%date:~4,2%%date:~7,2%%date:~10,4%
set var=%computername%_%dt%_%tm%.txt
echo %var%
Post by chanchoth
Many thanks for your assistance.
yw.

Timo Salmi
2005-07-25 06:49:05 UTC
Permalink
Post by chanchoth
echo %time:~0,2%%time:~3,2%%time:~6,2%
The problem is when the time is 1-9, it shows only hmmss. All I want is
when the time is from 1-9, I would like it to have 0 infront of the
time so that I can have this format hhmmss.
Your problem is covered e.g. in
130306 Jul 12 2005 ftp://garbo.uwasa.fi/pc/link/tscmd.zip
tscmd.zip Useful NT/2000/XP script tricks and tips, T.Salmi
and best discussed in news:alt.msdos.batch.nt

All the best, Timo
--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:***@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
Timo's FAQ materials at http://www.uwasa.fi/~ts/http/tsfaq.html
William Allen
2005-07-25 07:43:09 UTC
Permalink
"chanchoth" wrote in message
...snip
Post by chanchoth
I would like to create a simple batch file which can show the time in
showtime.bat
============
echo %time:~0,2%%time:~3,2%%time:~6,2%
The problem is when the time is 1-9, it shows only hmmss. All I want is
when the time is from 1-9, I would like it to have 0 infront of the
time so that I can have this format hhmmss.
You can use the str1=str2 substitute sytax to create a new variable
with any [Space]s replaced with zeros, then use you own syntax on
the new variable.

Lines that don't begin with two spaces have wrapped accidentally
====Begin cut-and-paste (omit this line)
@ECHO OFF
SETLOCAL
SET TM=%TIME: =0%
ECHO %TM:~0,2%%TM:~3,2%%TM:~6,2%

====End cut-and-paste (omit this line)
Simulated Win2000 for study/demo use. Cut-and-paste as Batch text file.
Batch file troubleshooting: http://www.allenware.com/find?UsualSuspects

--
William Allen
Free interactive Batch Course http://www.allenware.com/icsw/icswidx.htm
Batch Reference with examples http://www.allenware.com/icsw/icswref.htm
Header email is rarely checked. Contact us at http://www.allenware.com/
Loading...