0

I'm writing something in BATCH and I need to use GOTO's but instead of going to the correct GOTO it goes to the next label/class rather then the right one.

I've tried using it without a colon but still can't get a solution.

@echo off 
title Nexus Generator
color 0a 
goto :signin  

:signin
echo =====================================
echo Welcome to NEXUS GENERATOR!
echo Please enter your KEY!
echo =====================================
set /p id="Enter Key: "
if %id% == "Test" then goto :signedin else goto :error

:error
echo uh lol

:signedin
cls
echo lol
pause >nul

I expect that when TEST is entered it will go to signedin, and when anything else is entered it will go to error.

  • The strings on both sides of the comparison operator must match to gain a `True` condition, so you need to quote the %variable%. `if /i` makes the comparison case-insensitive – Magoo May 06 '19 at 04:23
  • So it would go like this instead? if /i %id% == "Test" then goto %signedin% else goto %error% – Cameron is Cool May 06 '19 at 04:28
  • I recommend to read the answers on [How to stop Windows command interpreter from quitting batch file execution on an incorrect user input?](https://stackoverflow.com/a/49834019/3074564) and [Symbol equivalent to NEQ, LSS, GTR, etc. in Windows batch files](https://stackoverflow.com/a/47386323/3074564). – Mofi May 06 '19 at 05:33
  • 1
    your variable needs to be quoted as well. `if "%id%" == "Test" goto..` and there is no `then` function in batch. – Gerhard May 06 '19 at 06:06
  • 2
    `if "%id%" == "Test" (goto :signedin) else (goto :error)` **then** is seen as an executable with arguments `goto :signedin else goto :error`. Without the **then** the instruction executed is `goto :signedin else goto :error` which *should* action the first `goto` and ignore the remainder. Parenthesising the `goto destination` clauses allows `cmd` to successfully resolve the ambiguous syntax. – Magoo May 06 '19 at 06:11
  • Simple solution: just write `if /I "%id%" == "Test" goto :sugnedin`, because if the condition is not fulfilled, execution continues at section `:error` anyway; nevertheless, type `if /?` and read the help text... – aschipfl May 06 '19 at 10:42

2 Answers2

1
  • You need compare some "in quotes" with some "in quotes" too,

  • goto :some place to avoid next line to be executed, like goto :eof

@echo off 

color 0a
title Nexus Generator

echo/=====================================
echo/Welcome to NEXUS GENERATOR^!
echo/Please enter your KEY^!
echo/=====================================

set /p "id=Enter Key: "
if /i "%id%" == "test" goto :signedin

:error
echo/uh lol
goto :eof

:signedin
cls 
echo/lol 
pause >nul

in one short version:

@echo off & color 0a 
title Nexus Generator
echo/=====================================
echo/Welcome to NEXUS GENERATOR^!
echo/Please enter your KEY^!
echo/=====================================
set /p "id=Enter Key: "
echo/%id%|findstr /lic:"test" 2>nul && goto :signedin
:error
echo/uh lol & goto :eof
:signedin
cls & echo/lol & pause >nul
Io-oI
  • 2,514
  • 3
  • 22
  • 29
  • When using this code, putting in something other then TEST crashed the program. – Cameron is Cool May 06 '19 at 05:00
  • Still crashing the program if I enter something other then TEST. – Cameron is Cool May 06 '19 at 05:07
  • I see, replace == to equ in if,, thank you for real time reply! – Io-oI May 06 '19 at 05:07
  • Followed your instructions and still crashing? – Cameron is Cool May 06 '19 at 05:14
  • Add some character to var, so, if user accidentally pres one key not characters, like one operator or something. Any suggest, I'm here, thank you! sorry my English. – Io-oI May 06 '19 at 17:33
  • `set /p` replaces the value (or leaves it unchanged if user just presses ENTER) - it does not concatenate. So a preset makes no sense in this case. And especially not a space - which you remove anyway with `%id: =%` (which is also not needed and possibly even harmful) – Stephan May 06 '19 at 19:59
0

Seeing as you only have 2 results you want to goto, I would say you do not need the else statement as the goto will only occur if the if statement matches, else it will simply fall through to the error. We however need to test each side of the operator to be equal. Currently you are testing Test == "Test". Note the one is quoted and the other not and you want to have an exact match, therefore you need to double quote your variable. "%id%"=="Test":

@echo off 
title Nexus Generator
color 0a 
goto :signin  

:signin
echo =====================================
echo Welcome to NEXUS GENERATOR!
echo Please enter your KEY!
echo =====================================
set /p id="Enter Key: "
if /i "%id%" == "Test" goto :signedin

echo Oops, you entered "%id%" instead of "Test"
goto :eof

:signedin
cls
echo You're now signed in!!
pause >nul

That said, I prefer not using goto statements unless completely needed, so we can just run code blocks after the if and else statements:

@echo off 
title Nexus Generator
color 0a 
goto :signin  

:signin
echo =====================================
echo Welcome to NEXUS GENERATOR!
echo Please enter your KEY!
echo =====================================
set /p id="Enter Key: "
if /i "%id%" == "Test" (
      cls
      echo You're now signed in!!
  ) else (
      cls
      echo Oops, you entered "%id%" instead of "Test"
)
pause >nul
Gerhard
  • 22,678
  • 7
  • 27
  • 43