2 Sep 2014 Hobart   » (Journeyer)

Windows technique to print timestamps before & after from the command line

On Unix, a quick way to output timestamps is:

$ date ; slowcommand ; date
Tue Sep  2 12:12:18 MDT 2014
Tue Sep  2 12:12:34 MDT 2014
$ 
But if you try a similar approach at the Windows command prompt, there's a few problems.
  • The command TIME /T outputs the time, but only in HH:MM format.
     
  • The command prompt's builtin magic variable %TIME% outputs HH:MM:SS.ss, but if you try it, the results are unexpected:
    C:\>echo %TIME% && SLOWCOMMAND && echo %TIME%
    13:42:05.10
    13:42:05.10

    C:\>
    The timestamps come out the same, because the command prompt does all variable substitution in a line at once, before executing the first command.

    In batch files, this can be mitigated with the setting ENABLEDELAYEDEXPANSION and referring to variables !LIKETHIS! instead of %LIKETHIS%. But that won't work at the command prompt.
The solution I used was to run the command explicitly afterwards with CMD /C , using the ^ to escape out the % character:
C:\>echo %TIME% && SLOWCOMMAND && cmd /c echo %TIME^%
13:51:27.58
13:51:46.66

C:\>
Other solutions welcome.

Syndicated 2014-09-02 20:18:46 from jon's blog

Latest blog entries     Older blog entries

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!