I usually find that conditional statement concatenation operators are much more convenient than ERRORLEVEL.
yourCommand && ( echo yourCommand was successful ) || ( echo yourCommand failed )
There is one complication you should be aware of. The error branch will fire if the last command in the success branch causes an error.
yourCommand && ( someCommandThatMayFail ) || ( echo This will fire if yourCommand or someCommandThatMayFail raises an error )
The fix is ββto insert a secure team that is guaranteed to be successful at the end of the success branch. I like to use (call ) , which does nothing, except that ERRORLEVEL is 0. There is a consequence (call) , which does nothing, except setting ERRORLEVEL to 1.
yourCommand && ( someCommandThatMayFail (call ) ) || ( echo This can only fire if yourCommand raises an error )
See Incorrect way to check for an unnecessary (error) return code in a Windows batch file for examples of the subtleties required when using ERRORLEVEL to detect errors.
dbenham Jun 13 '13 at 11:27 2013-06-13 11:27
source share