Executing multiple commands from a Windows cmd script
Solution 1
When you call another .bat file, I think you need "call" in front of the call:
call otherCommand.bat
Solution 2
You can use the && symbol between commands to execute the second command only if the first succeeds. More info here http://commandwindows.com/command1.htm
Solution 3
Not sure why the first command is stopping. If you can make it parallel, you can try something like
start cmd.exe /C 1.bat
start cmd.exe /C 2.bat
Solution 4
I have just been doing the exact same(ish) task of creating a batch script to run maven test scripts. The problem is that calling maven scrips with mvn clean install ... is itself a script and so needs to be done with call mvn clean install.
Code that will work
rem run a maven clean install
cd C:\rbe-ui-test-suite
call mvn clean install
rem now run through all the test scripts
call mvn clean install -Prun-integration-tests -Dpattern=tc-login
call mvn clean install -Prun-integration-tests -Dpattern=login-1
Note rather the use of call. This will allow the use of consecutive maven scripts in the batch file.
Solution 5
Using double ampersands will run the second command, only if the first one succeeds:
cd Desktop/project-directory && atom .
Where as, using only one ampersand will attempt to run both commands, even if the first fails:
cd Desktop/project-directory & atom .
Darren Greaves
Freelance software developer living in the UK. http://boncey.org/ http://twitter.com/boncey https://github.com/boncey
Updated on July 08, 2022Comments
-
Darren Greaves almost 2 years
I'm trying to write a Windows cmd script to perform several tasks in series. However, it always stops after the first command in the script.
The command it stops after is a maven build (not sure if that's relevant).
How do I make it carry on and run each task in turn please?
Installing any software or configuring the registry etc is completely out of the question - it has to work on a vanilla Windows XP installation I'm afraid.
Ideally I'd like the script to abort if any of the commands failed, but that's a "nice to have", not essential.
Thanks.
-
Darren Greaves over 15 yearsThanks but I am unable to install any software. I wish I had access to something more powerful than the crappy Windows scripting language. :-(
-
Ferruccio over 15 yearsYou can always use vbscript or javascript. They're built into the Windows scripting host.
-
Darren Greaves over 15 yearsHi, in the original script I wasn't calling other .cmd files, but I have since split it into separate files so I could run each in turn. So, putting call in front of each command seems to have done the trick, thanks!
-
Darren Greaves over 15 yearsThanks, I'll give that a try in conjunction with the accepted answer above.
-
JellicleCat about 13 yearsUse
/K
instead of/C
if you want the shell to remain open after your command has executed. -
jfpoilpret over 11 yearsAs a matter of fact, on Windows,
mvn
is a .bat file by itself, thus you need to usecall
for it as incall mvn install
; normally you don't need to create an extra cmd file. -
Varun Achar about 11 yearsWill this stop the execution of the second script if maven build fails?
-
kevinji almost 11 yearsThat link is broken: "Backend server did not respond in time. App server is too busy and cannot handle requests in time."
-
user66001 over 10 yearsNo need to use
cmd.exe
andstart
, juststart x.bat1
will do. -
Mitja almost 10 yearsCan someone explain to me why
calc && echo foo
results in the calculator being started and "foo" being printed immediately? How can I startcalc
in a way that it blocks the windows shell like the bash does without&
? -
coderforlife almost 10 years@TheM Windows distinguishes between GUI-based and command-based applications (there is a flag in near the beginning of the EXE file). If you start a GUI-based application from the command line it always appears to end immediately since it is completely detached from the command-line. If you start a command-based program from a GUI program (like Explorer) it will always show a new command line. POSIX systems make no such distinction thus the behavior is more consistent.
-
coderforlife almost 10 years@TheM To get around this you can use
start /B /WAIT calc
-
Mitja almost 10 years@coderforlife I agree that this behavior is somewhat inconsistent but at least there is a way to do it. Thank you!
-
Pushkar almost 10 yearsNote that you don't need semicolons in batch files. And the reason why you need to use call is that mvn itself is a batch file and batch files need to call each other with call, otherwise control does not return to the caller.
-
Ng2-Fun almost 8 years@Gulzar Nazim - Do you know if I can put all the commands in a single batch file with if-else statement as I don't want to maintain it in different batch files?
-
Giridhar Karnik about 7 yearsThe irony here is the answer is pointing in the opposite direction of the question and still it gets 120 upvotes
-
joshden about 7 yearsI was trying to do something similar with
npm
updates andprotractor
tests. I didn't think this answer applied to me until I realizednpm
andprotractor
are.cmd
files on Windows.