Executing multiple commands from a Windows cmd script

282,857

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 .
Share:
282,857
Darren Greaves
Author by

Darren Greaves

Freelance software developer living in the UK. http://boncey.org/ http://twitter.com/boncey https://github.com/boncey

Updated on July 08, 2022

Comments

  • Darren Greaves
    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
    Darren Greaves over 15 years
    Thanks but I am unable to install any software. I wish I had access to something more powerful than the crappy Windows scripting language. :-(
  • Ferruccio
    Ferruccio over 15 years
    You can always use vbscript or javascript. They're built into the Windows scripting host.
  • Darren Greaves
    Darren Greaves over 15 years
    Hi, 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
    Darren Greaves over 15 years
    Thanks, I'll give that a try in conjunction with the accepted answer above.
  • JellicleCat
    JellicleCat about 13 years
    Use /K instead of /C if you want the shell to remain open after your command has executed.
  • jfpoilpret
    jfpoilpret over 11 years
    As a matter of fact, on Windows, mvn is a .bat file by itself, thus you need to use call for it as in call mvn install; normally you don't need to create an extra cmd file.
  • Varun Achar
    Varun Achar about 11 years
    Will this stop the execution of the second script if maven build fails?
  • kevinji
    kevinji almost 11 years
    That link is broken: "Backend server did not respond in time. App server is too busy and cannot handle requests in time."
  • user66001
    user66001 over 10 years
    No need to use cmd.exe and start, just start x.bat1 will do.
  • Mitja
    Mitja almost 10 years
    Can someone explain to me why calc && echo foo results in the calculator being started and "foo" being printed immediately? How can I start calc in a way that it blocks the windows shell like the bash does without &?
  • coderforlife
    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
    coderforlife almost 10 years
    @TheM To get around this you can use start /B /WAIT calc
  • Mitja
    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
    Pushkar almost 10 years
    Note 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
    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
    Giridhar Karnik about 7 years
    The irony here is the answer is pointing in the opposite direction of the question and still it gets 120 upvotes
  • joshden
    joshden about 7 years
    I was trying to do something similar with npm updates and protractor tests. I didn't think this answer applied to me until I realized npm and protractor are .cmd files on Windows.