46

I have a .bat file in windows that does three things

cmd1 arg1 arg2
cmd2 arg3 
cmd3 arg4 arg5 arg6

Sometimes cmd1 can fail and that's fine, I would like to carry on and execute cmd2 and cmd3. But my bat stops at cmd1. How can I avoid this?

Update for clarity - these are not other .bat files, they are exe commands. Hopefully I don't have to build a tree of .bat files just to achieve this.

ConfusedNoob
  • 857
  • 2
  • 12
  • 18
  • You should be able to start the command using [`cmd /C`](http://stackoverflow.com/a/74321/259953). – Oliver Salzburg Jan 03 '13 at 22:49
  • 1
    You need to show a specific code example of what is not working. Batch files normally do not terminate automatically if a command fails with an error. Batch files do terminate if there is a syntax error. If your code is exiting upon an exe error, then there must be logic in your code that is causing that behavior. – dbenham Jan 04 '13 at 14:59

4 Answers4

36

Another option is to use the amperstand (&)

cmd1 & cmd2 & cmd3

If you use a double, it only carries on if the previous command completes successfully (%ERRORLEVEL%==0)

cmd1 && cmd2 && cmd3

If you use a double pipe (||), it only runs the next command if the previous completes with an error code (%ERRORLEVEL% NEQ 0)

cmd1 || cmd2 || cmd3

Canadian Luke
  • 24,199
  • 39
  • 117
  • 171
  • Can I do this when passing args also? I just tried and it didn't see to work. – ConfusedNoob Jan 03 '13 at 22:39
  • @ConfusedNoob I do all the time... I have a `CHKDSK` command, with arguments, that works. I type `check D: & check E: & check F:`, and it works. The arguments the BAT file has include the `/perf /f /r /b /scan` switches – Canadian Luke Jan 03 '13 at 23:14
  • 2
    good one! brought me an idea how to implement several retries: cmd || cmd || ... will execute next cmd until succeeds – iTake Nov 11 '13 at 17:21
  • 2
    This makes long scripts unreadable, is there no other way? – jan Oct 10 '14 at 07:53
  • @Jan how does it make long scripts unreadable? – Canadian Luke Oct 10 '14 at 14:09
  • @CanadianLuke because 100 commands in 100 lines need to be concatenated to one big line. No empty lines for separation of blocks of code can be used. – jan Oct 13 '14 at 13:46
  • If you're running a hundred programs from one batch file, I'd question the reason, @Jan. There are other ways of doing it, but for only a few commands, this is my preferred way. – Canadian Luke Oct 13 '14 at 13:50
  • 1
    @CanadianLuke Well batch is the windows batch framework. How do you run hundreds of commands non interactively? Is there a better approach in the power shell? Is that worse to move there? – jan Oct 13 '14 at 13:54
  • There are many other ways, but you'd be splitting up the sets of commands into different scripts. Write scripts that do a few commands amazingly, then call them from other scripts. When I image systems, I run a batch file that installs extra programs, runs vbs scripts, runs other batch files, runs PowerShell scripts, and they all do their specific jobs perfectly – Canadian Luke Oct 13 '14 at 15:08
  • that helps my node start command in package.json thanks – Xin Jan 19 '17 at 00:59
22

This worked for me:

cmd.exe /c cmd1 arg1
cmd2
...

This uses cmd.exe to execute the command in a new instance of the Windows command interpreter, so a failed command doesn't interrupt the batch script. The /c flag tells the interpreter to terminate as soon as the command finishes executing.

cmd2 executes even if the first command fails. See cmd /? from Windows Command Prompt for more information.

Joseph238
  • 321
  • 2
  • 5
17

Presumming the cmds are other .bat files stack the commands like this:

 call cmd1
 call cmd2
 call cmd3
mdpc
  • 4,429
  • 9
  • 28
  • 36
1

To add to Canadian Luke's answer, if you want to keep your script a little more organized you can use ^ to continue the command chain in a new line in your script, like this:

scoop install windows-terminal & ^
scoop install joplin & ^
scoop install neovim & ^
scoop install autohotkey & ^
... & ^
scoop update -a

Instead of the ugly

scoop install windows-terminal & scoop install joplin & scoop install neovim & ...
exic
  • 469
  • 5
  • 9