120

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Raghuram
  • 3,937
  • 2
  • 19
  • 25
  • 4
    Possible duplicate of [How to run two commands in one line in Windows CMD?](http://stackoverflow.com/questions/8055371/how-to-run-two-commands-in-one-line-in-windows-cmd) – IvanRF Oct 28 '15 at 19:42

2 Answers2

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
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 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) – Raghuram Jan 19 '12 at 07:20
  • 1
    That's because the environment variables are evaluated when the command is _read_ rather than executed. I'll update the answer. – paxdiablo Jan 19 '12 at 07:24
  • 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 – Raghuram Jan 19 '12 at 07:29
  • 3
    I tended to use `time /t` (or `echo.|time`) instead of `echo %time%` to circumvent the issue. – Joey Jan 19 '12 at 07:32
  • @Joey, that will work since it's not an environment variable. But you should be using `!time! rather than `%time%` - the former is the one with deferred behaviour. – paxdiablo Jan 19 '12 at 07:33
  • Yes, but then I need a new `cmd` with `/v:on`. I'm lazy :P (and I have long used [`timethis`](http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=3087) for this anyway) – Joey Jan 19 '12 at 07:36
  • Thanks paxdiablo and joey . I used both of your ideas and able to get the time taken – Raghuram Jan 19 '12 at 09:16
  • Also before i saw your answer i wrote a small batch file like this echo %time% %* echo %time and passed the script which i need to execute with arguments as an argument to this batch script and achieved the required output – Raghuram Jan 19 '12 at 09:18
  • 1
    Any idea how to get %errorlevel% to be correct on following calls? – BradLaney Mar 22 '17 at 06:59
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.

npocmaka
  • 55,367
  • 18
  • 148
  • 187