0

With batch files, I'm trying to run commands, if the command returns an error try other commands with GOTO for each one...however the script keeps failing with an error....but it shouldn't right now at least if the last GOTO doesn't work it should just go into an endless loop.

I've read that checking if there is an error is this...

IF %ERRORLEVEL% NEQ 0 (
    GOTO tryInRoot
)

But I'm not quite sure, any help?

grunt precommit
IF %ERRORLEVEL% NEQ 0 (
    GOTO tryInRoot
)

:tryInRoot
    cd mow\client && grunt precommit
    IF %ERRORLEVEL% NEQ 0 (
        GOTO tryInTrunk
    )

:tryInTrunk
    cd client && grunt precommit
        IF %ERRORLEVEL% NEQ 0 (
        GOTO whenInMapManager
    )

:whenInMapManager
    cd ..
    IF EXIST "Gruntfile.js" (
        GOTO leavingMapManager
    ) ELSE (
        GOTO whenInMapManager
    )
:leavingMapManager
    cd ..
    cd mow\client && grunt precommit
Carson
  • 1,147
  • 5
  • 19
  • 41
  • 3
    Your logic is not making much sense. Even if the errorlevel is zero it will still run :tryInRoot and :tryIntrunk and :whenInMapManager. You need logic to have it skip over that code if the errorlevel is zero. – Squashman Jun 19 '18 at 15:38
  • @Squashman ah so that's just simply adding ELSE blocks for those cases to exit ? `exit \b` – Carson Jun 19 '18 at 15:40
  • Note that many commands set [`ErrorLevel`](http://ss64.com/nt/errorlevel.html) but do *not* reset them, so even if you corrected your logic as already suggested (by adding `else goto :somewhere`, for example), `ErrorLevel` might remain a non-zero value upon the first exception. As an alternative to the `if %ErrorLevel%` queries, you could also use the [conditional execution operators `&&` and `||`](http://ss64.com/nt/syntax-redirection.html), which check the *exit code* of the preceding command... – aschipfl Jun 19 '18 at 15:44
  • I started with that, but if the first three fail I needed to add a GOTO to do a loop of cd out of a directory until a certain point...basically. `grunt precommit || cd mow\client && grunt precommit || cd client && grunt precommit || GOTO whenInMapManager` So I'm guessing using the conditional execution operators doesn't work with GOTOs or my logic is wrong elsewhere ? – Carson Jun 19 '18 at 15:47
  • You should use parentheses in the above case, e.g. `grunt precommit || (cd mow\client & grunt precommit) || (cd client & grunt precommit) || GOTO whenInMapManager`. _Noting that you'd be running the last precomnit in `.\mow\client\client`_ – Compo Jun 19 '18 at 16:40
  • See [Single line with multiple commands using Windows batch file](https://stackoverflow.com/a/25344009/3074564) which also explains the better syntax for evaluating the exit code of a command or application working since MS-DOS even in command blocks without usage of variable expansion: `if errorlevel X` and `if not errorlevel X`. Help output on running `if /?` in a command prompt window explains this syntax working since 30 years now. – Mofi Jun 19 '18 at 17:28

1 Answers1

0

Using || is a better option...

However, GOTO has a unique case where it will break ||, so you need to add CALL prior to this.

CALL grunt precommit || cd mow\client && grunt precommit || cd client && grunt precommit || GOTO whenInMapManager

Carson
  • 1,147
  • 5
  • 19
  • 41