<http://www.netikka.net/tsneti/info/tscmd016.htm>
Copyright © 2003-2010 by Prof. Timo Salmi  
Last modified Sun 25-Apr-2010 20:34:27

 
Assorted NT/2000/XP/.. CMD.EXE Script Tricks
From the html version of the tscmd.zip 1cmdfaq.txt file
To the Description and the Index
 

This page is edited from the 1cmdfaq.txt faq-file contained in my tscmd.zip command line interface (CLI) collection. That zipped file has much additional material, including a number of detached .cmd script files. It is recommended that you also get the zipped version as a companion.

Please see "The Description and the Index page" for the conditions of usage and other such information.



16} How can I get the time without having to press enter?

You might first wish to revisit item #1.

Independently of the OS version
  @echo off
  echo.| time | find /v "new"

For example you might get
  The current time is: 8:46:29.15

However, in XP scripting the date and time variables are directly available
  @echo off
  echo %time%

In this option you might get for example
  D:\TEST>cmdfaq
   9:03:13.56

Also
  @echo off
  time /t

which would produce (only) e.g.
  D:\TEST>cmdfaq
  09:03

There is a complication, however. Say that you wish to capture the hour, alone. One has to take into account that the hour might have one or two digits. One can't safely use %time:~0,2% so instead use the for /f method:
  @echo off & setlocal enableextensions
  for /f "tokens=1 delims=:. " %%h in ("%time%") do set hh_=%%h
  echo %hh_%
  endlocal & goto :EOF

The output would be e.g.
  D:\TEST>cmdfaq
  9

If you want the potential leading zero, use TIME /T as follows (or pad it as explained in item #127)
  @echo off & setlocal enableextensions
  for /f "tokens=1 delims=:. " %%h in ('time /t') do set hh_=%%h
  echo %hh_%
  endlocal & goto :EOF

The output would be e.g.
  D:\TEST>cmdfaq
  09

Octals are (confusingly) identified by a leading zero. Therefore consider carefully if and when wish to have the leading zero. For example, if you add to the above
  set /a hhplus1_=%hh_%+1
you'll get
  C:\_D\TEST>cmdfaq
  09
  Invalid number. Numeric constants are either decimal (17),
  hexadecimal (0x11), or octal (021).

Another option
  @echo off & setlocal enableextensions
  ::
  :: Put the time in an auxiliary variable to fix the moment

  set time_=%time%
  ::
  :: Take care of the potential leading space for small hours

  set time_=%time_: =0%
  ::
  :: Extract the time elements

  set hh_=%time_:~0,2%
  set mn_=%time_:~3,2%
  set ss_=%time_:~6,2%
  set s100_=%time_:~9,2%
  ::
  :: Display

  echo %hh_% %mn_% %ss_% %s100_%
  endlocal & goto :EOF

The output might be e.g.
  D:\TEST>cmdfaq
  09 58 51 06

There are other alternatives, including a Visual Basic Script aided command line script like the one already presented in item #1.