121

In Unix, we can put multiple commands in a single line like this:

$ date ; ls -l ; date 

I tried a similar thing in Windows:

 > echo %TIME% ; dir ; echo %TIME 

But it printed the time and doesn't execute the command dir.

How can I achieve this?

1

2 Answers 2

200

Use:

echo %time% & dir & echo %time% 

This is, from memory, equivalent to the semi-colon separator in bash and other UNIXy shells.

There's also && (or ||) which only executes the second command if the first succeeded (or failed), but the single ampersand & is what you're looking for here.


That's likely to give you the same time however since environment variables tend to be evaluated on read rather than execute.

You can get round this by turning on delayed expansion:

pax> cmd /v:on /c "echo !time! & ping 127.0.0.1 >nul: & echo !time!" 15:23:36.77 15:23:39.85 

That's needed from the command line. If you're doing this inside a script, you can just use setlocal:

@setlocal enableextensions enabledelayedexpansion @echo off echo !time! & ping 127.0.0.1 >nul: & echo !time! endlocal 
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks for the answer. Issue is it works partially. the first and last %time% prints the same time even when the command (in middle) takes atleast 40 seconds to complete.( I run my script instead of dir command)
That's because the environment variables are evaluated when the command is read rather than executed. I'll update the answer.
Still the issue persists. The script is exiting with code 1. But still the second echo prints the same time See this op 12:57:56.93 A thread exited while 3 threads were running. 12:57:56.93
I tended to use time /t (or echo.|time) instead of echo %time% to circumvent the issue.
Any idea how to get %errorlevel% to be correct on following calls?
|
0

Can be achieved also with scriptrunner

ScriptRunner.exe -appvscript demoA.cmd arg1 arg2 -appvscriptrunnerparameters -wait -timeout=30 -rollbackonerror -appvscript demoB.ps1 arg3 arg4 -appvscriptrunnerparameters -wait -timeout=30 

Which also have some features as rollback , timeout and waiting.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.