how can I make my .bat file continue after an error

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.


Solution 1:

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

Solution 2:

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

 call cmd1
 call cmd2
 call cmd3

Solution 3:

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.