as the title said I need to run exactly two commands in one line using cmd in windows 10. How is it possible?
-
1Possible duplicate of [Multiple commands on a single line in a Windows batch file](https://stackoverflow.com/questions/8922224/multiple-commands-on-a-single-line-in-a-windows-batch-file) – aschipfl May 17 '18 at 15:41
-
Possible duplicate of [How do I run two commands in one line in Windows CMD?](https://stackoverflow.com/questions/8055371/how-do-i-run-two-commands-in-one-line-in-windows-cmd) – mazzy May 28 '18 at 07:28
3 Answers
to run two commands use &
. Both commands will be executed:
dir file.txt & echo done
Use &&
to execute the second command only if the first command was successful:
dir existentfile.txt && echo done
Use ||
to run the second command only if the first command failed:
dir nonexistentfile.txt || echo not found
You can combine:
dir questionablefile.txt && (echo file exists) || (echo file doesn't exist)

- 53,940
- 10
- 58
- 91
Easy to pick one from general syntax:
Run multiple commands (cmd1, cmd2, cmd3) in one line:
cmd1 & cmd2 & cmd3
// run all commands from left to right (& => all)
cmd1 && cmd2 && cmd3
// run all commands from left to right, stop at 1st fail (&& => till fail)
cmd1 | cmd2 | cmd3
// run all commands from left to right, stop at 1st fail, also | is pipe which sends cmd1 output to cmd2 & so on, so use when if you want to pass outputs to other commands - (| => till fail + pass output of left to right)
cmd1 || cmd2 || cmd3
// run all commands from left to right, stop at 1st success (|| => till good)
Summary:
& => run all
&& => run L2R till fail
| => run L2R till fail + pass output of left to right
|| => run L2R till good
where, L2R is left to right
Hope that helped.

- 25,399
- 9
- 157
- 140
cmd1;cmd2
cmd1&cmd2
cmd1|cmd2

- 1,025
- 1
- 13
- 26
-
3First line won't run (ignoring the second one or giving syntax errors - depending on the actual commands). Third line pipes the output of the first command to the second one. (Second line is the way to go). – Stephan May 17 '18 at 15:05
-
First line is error in cmd and correct in powershell. I'm sorry. Second and Third lines exaclty match to author request 'I need to run exactly two commands in one line using cmd in windows 10'. And thanks Stepan for && and || cases. But last cases does not match to '**exactly** two commands' ) – mazzy May 17 '18 at 19:51