Score:3

echo %time% duplicate outputs

ch flag

I have a command that adds the timestamp at the beginning and the end (echo %time% & #other command# & echo %time%). However, the %time% will be the same, regardless of how long the command took to execute.

example output:

Time start: 19:48:31.75

Pinging google.com [2a00:1450:400e:80c::200e] with 32 bytes of data:
Reply from 2a00:1450:400e:80c::200e: time=13ms
Reply from 2a00:1450:400e:80c::200e: time=13ms
Reply from 2a00:1450:400e:80c::200e: time=9ms
Reply from 2a00:1450:400e:80c::200e: time=10ms

Ping statistics for 2a00:1450:400e:80c::200e:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 9ms, Maximum = 13ms, Average = 11ms

Time end: 19:48:31.76

Is there a way to have this work, still within one line?

Thanks

djdomi avatar
za flag
command & command execution is only when true, why not use semicolon ; it execute regardless
ch flag
@djdomi i tried this, but writing it like `echo %time%; ping google.com; echo %time%` or even with quotes, it doesn't work. It sees the semicolon as a part of the command, rather than closing it
in flag
@djdomi `;` does no work in cmd, a single `&` continues execution while `&&` does not
jp flag
@MichaelHampton: The difference is .01 seconds while it shoud be more than 4 seconds.
Score:2
in flag

CMD expands variables and then executes each line. That means that

echo %time% & ping 127.0.0.1 & echo %time%

is first expanded, and then executed

There is also "delayed expansion" Here is some example usage, and even more detailed This however only works in a cmd file, and not on commandline

setlocal ENABLEDELAYEDEXPANSION
echo !time! & ping 127.0.0.1 & echo !time!

Continue searching maybe cmd /V can be used:

cmd /V /C "echo %time% & ping 127.0.0.1 & echo !time!"

It gives expected result for me, but there might be gotchas preventing some use cases.

It Wasn't Me avatar
cn flag
*[... and even more detailed](https://superuser.com/questions/1569594/how-does-delayed-expansion-works-in-batch-script)* I liked the support reference.... d:)
It Wasn't Me avatar
cn flag
I am in my mac now, so no way to tes, but. maybe **`call`** can do this too: `echo %time% & ping 127.0.0.1 & call echo %time%`...
in flag
@It that will not work since the variables is evaluated first there as well, you must have the delayed expansion if you run it on same line (or block `()`)
It Wasn't Me avatar
cn flag
for this i write *maybe*
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.