batch-fileservicerestarterrorlevel

Check for error on process stop


I want to restart a service, and if I get error level 5 I want to reboot the machine. but when I try it just exits the script.

NET STOP TeamViewer9 && NET START TeamViewer9

IF %ERRORLEVEL%==1(
    ECHO proceed to restart...
    PAUSE
    SHUTDOWN /F /R /T 30
    )

Solution

  • You are comparing with a value with an opening parenthesis in it;

    IF %ERRORLEVEL%==1(
    

    is the same as

    IF "%ERRORLEVEL%"=="1(" 
    

    and that's not what you want. Just insert a space before the ( so it is related to the IF statement and not the 1) :

    IF %ERRORLEVEL%==1 (
    

    Batch language is a bit easy to fool; it really should have complained about a missing left parenthesis rather than skipping the rest of the code.