48

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.

4 Answers4

37

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,640
24

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
  • 341
20

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

 call cmd1
 call cmd2
 call cmd3
mdpc
  • 4,489
2

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
  • 519