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.

1

8 Answers 8

145

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

call otherCommand.bat
Sign up to request clarification or add additional context in comments.

6 Comments

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!
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.
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.
@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?
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.
|
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

7 Comments

Thanks, I'll give that a try in conjunction with the accepted answer above.
Will this stop the execution of the second script if maven build fails?
That link is broken: "Backend server did not respond in time. App server is too busy and cannot handle requests in time."
@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.
@TheM To get around this you can use start /B /WAIT calc
|
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

2 Comments

Use /K instead of /C if you want the shell to remain open after your command has executed.
No need to use cmd.exe and start, just start x.bat1 will do.
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.

Comments

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 .

Comments

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

Comments

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.

2 Comments

Thanks but I am unable to install any software. I wish I had access to something more powerful than the crappy Windows scripting language. :-(
You can always use vbscript or javascript. They're built into the Windows scripting host.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.