JSI Tip 6394. How can I test if a batch command failed or succeeded?

I introduced conditional processing symbols in tip 0290:

The double ampersand (&&) causes the command following this symbol to run only if the command preceding the symbol is successful.

The double pipe (||) causes the command following this symbol to run only if the command preceding the symbol fails.

Examples

pushd \\ServerName\ShareName||goto NoAccess

If the command fails, goto NoAccess will be executed.

pushd \\ServerName\ShareName&&goto popit
@echo The user has no access
goto :EOF
:popit
REM Do stuff while the current folder is \\ServerName\ShareName
popd

If the command is successful, goto popit is executed, otherwise the error is echoed.



Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish