Executing multiple commands from a Windows cmd script

WindowsBatch FileScriptingCmd

Windows Problem Overview


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.

Windows Solutions


Solution 1 - Windows

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

call otherCommand.bat

Solution 2 - Windows

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 - Windows

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 - Windows

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 - Windows

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 .

Solution 6 - Windows

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.

Solution 7 - Windows

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

Solution 8 - Windows

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDarren GreavesView Question on Stackoverflow
Solution 1 - WindowsLou FrancoView Answer on Stackoverflow
Solution 2 - WindowsSteveView Answer on Stackoverflow
Solution 3 - WindowsGulzar NazimView Answer on Stackoverflow
Solution 4 - Windowsmhollander38View Answer on Stackoverflow
Solution 5 - WindowsJSON C11View Answer on Stackoverflow
Solution 6 - WindowsMr FoozView Answer on Stackoverflow
Solution 7 - WindowsPulendar VaddeView Answer on Stackoverflow
Solution 8 - WindowsPushkarView Answer on Stackoverflow