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 fileaschipfl– aschipfl2018-05-17 15:41:56 +00:00Commented May 17, 2018 at 15:41
-
Possible duplicate of How do I run two commands in one line in Windows CMD?mazzy– mazzy2018-05-28 07:28:14 +00:00Commented May 28, 2018 at 7: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)
Comments
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.
Comments
cmd1;cmd2
cmd1&cmd2
cmd1|cmd2