4

as the title said I need to run exactly two commands in one line using cmd in windows 10. How is it possible?

2

3 Answers 3

12

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)
Sign up to request clarification or add additional context in comments.

Comments

7

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

-2

cmd1;cmd2

cmd1&cmd2

cmd1|cmd2

2 Comments

First 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).
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' )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.