141

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
  • 3,326
  • 4
  • 29
  • 32

8 Answers8

142

When you call another .bat file, I think you need "call" in front of the call:

call otherCommand.bat
Gulzar Nazim
  • 51,744
  • 26
  • 128
  • 170
Lou Franco
  • 87,846
  • 14
  • 132
  • 192
  • 1
    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 Oct 13 '08 at 15:48
  • 6
    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. – jfpoilpret Sep 13 '12 at 08:17
  • 2
    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. – Pushkar Jul 25 '14 at 08:31
  • @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? – Ng2-Fun Aug 26 '16 at 19:49
  • The irony here is the answer is pointing in the opposite direction of the question and still it gets 120 upvotes – Giridhar Karnik Apr 20 '17 at 14:05
  • 1
    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. – joshden Apr 20 '17 at 20:32
45

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

Steve
  • 481
  • 3
  • 3
  • Thanks, I'll give that a try in conjunction with the accepted answer above. – Darren Greaves Oct 13 '08 at 15:49
  • Will this stop the execution of the second script if maven build fails? – Varun Achar May 14 '13 at 07:07
  • That link is broken: "Backend server did not respond in time. App server is too busy and cannot handle requests in time." – Kevin Ji Jul 16 '13 at 19:17
  • 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 `&`? – Mitja Jun 25 '14 at 00:02
  • 1
    @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 Jul 02 '14 at 17:48
  • 2
    @TheM To get around this you can use `start /B /WAIT calc` – coderforlife Jul 02 '14 at 17:56
  • @coderforlife I agree that this behavior is somewhat inconsistent but at least there is a way to do it. Thank you! – Mitja Jul 04 '14 at 02:18
25

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
Gulzar Nazim
  • 51,744
  • 26
  • 128
  • 170
23

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.

mhollander38
  • 775
  • 3
  • 11
  • 22
6

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 .
JSON C11
  • 11,272
  • 7
  • 78
  • 65
1

I don't know the direct answer to your question, but if you do a lot of these scripts, it might be worth learning a more powerful language like perl. Free implementations exist for Windows (e.g. activestate, cygwin). I've found it worth the initial effort for my own tasks.

Edit:

As suggested by @Ferruccio, if you can't install extra software, consider vbscript and/or javascript. They're built into the Windows scripting host.

Mr Fooz
  • 109,094
  • 6
  • 73
  • 101
  • Thanks but I am unable to install any software. I wish I had access to something more powerful than the crappy Windows scripting language. :-( – Darren Greaves Oct 13 '08 at 15:34
  • 6
    You can always use vbscript or javascript. They're built into the Windows scripting host. – Ferruccio Oct 13 '08 at 15:42
1

If you are running in Windows you can use the following command.

Drive:

cd "Script location"
schtasks /run /tn "TASK1"
schtasks /run /tn "TASK2"
schtasks /run /tn "TASK3"
exit
Anton K
  • 4,658
  • 2
  • 47
  • 60
0

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.

Pushkar
  • 727
  • 1
  • 7
  • 21