How to run multiple commands one after another in cmd

Run multiple commands one after another in cmd

Try using the conditional execution & or the && between each command either with a copy and paste into the cmd.exe window or in a batch file.

Additionally, you can use the double pipe || symbols instead to only run the next command if the previous command failed.

Execute command2 after execution of command1 has finished

ncrack --user Admin -P pass1.txt <IPAddress>:3389 -oN good.txt -f & ncrack --user Admin -P pass2.txt <IPAddress>:3389 -oN good.txt -f & ncrack --user Admin -P pass3.txt <IPAddress>:3389 -oN good.txt -f

Execute command2 only if execution of command1 has finished successfully

ncrack --user Admin -P pass1.txt <IPAddress>:3389 -oN good.txt -f && ncrack --user Admin -P pass2.txt <IPAddress>:3389 -oN good.txt -f && ncrack --user Admin -P pass3.txt <IPAddress>:3389 -oN good.txt -f

Execute command2 only if execution of command1 has finished unsuccessfully

ncrack --user Admin -P pass1.txt <IPAddress>:3389 -oN good.txt -f || ncrack --user Admin -P pass2.txt <IPAddress>:3389 -oN good.txt -f || ncrack --user Admin -P pass3.txt <IPAddress>:3389 -oN good.txt -f

Supporting Resources

  • Conditional Execution [1]
  • Conditional Execution [2]

Use below syntax in your cmd file.

call command1    
call command2
.    
.    
call commandx

Example:

call mvn install:install-file -Dfile=spring.jar -DgroupId=com.td.tdi.creditProtection.webservice -DartifactId=spring -Dversion=1.0 -Dpackaging=jar
call mvn install:install-file -Dfile=com.ibm.ws.prereq.jaxrs.jar -DgroupId=com.td.tdi.creditProtection.webservice -DartifactId=com.ibm.ws.prereq.jaxrs -Dversion=1.0 -Dpackaging=jar 
call mvn install:install-file -Dfile=com.ibm.ws.runtime.jar -DgroupId=com.td.tdi.creditProtection.webservice -DartifactId=com.ibm.ws.runtime -Dversion=1.0 -Dpackaging=jar
call mvn install:install-file -Dfile=IMSConnection_Utilities.jar -DgroupId=com.td.tdi.creditProtection.webservice -DartifactId=IMSConnection_Utilities -Dversion=1.0 -Dpackaging=jar

You can enter both commands on the same line and separate them with either a single ampersand (which causes them to be run in sequence) or two ampersands (which introduces simple error checking: the second command only runs if the first one was successful)

e.g.:

ncrack --user Admin -P pass1.txt <IPAddress>:3389 -oN good.txt -f && ncrack --user Admin -P pass2.txt <IPAddress>:3389 -oN good.txt -f

Source


Just add all the commands line by line in a batch file, and save the file as somename.bat.

Execute that batch file; all the commands would run sequentially in the order of their presence in the file.

How to execute the batch file through cmd:

path/to/the/directory/of/your/batchfile/somename.bat