Briant
Briant

Reputation: 85

If a variable equals a number goto

If a variable equals, for example 1 then goto to start1 BUT if the same variable equals 2 then goto to start2.

This is what i have so far:

if %method% == "1" (goto start1)
if %method% == "2" (goto start2)

:start1
echo start1
pause
exit

:start2
echo start2
pause
exit

But even if the method variable is equals 2 it always echo me start1...

Upvotes: 8

Views: 40831

Answers (3)

mordecai
mordecai

Reputation: 71

Having many if statements is kind of not neat ..have a counter for your comparing if statement. This should do the trick, I guess...

@echo off

Setlocal enabledelayedexpansion

Set returned_value=0

Set /p method= enter value:

: begin

For %%i in (*) do (
  Call :next_number returned_value
  If "!method!"=="!returned_value!" (
    Goto start!returned_value!
  )
)

Goto begin

:next_number

Set /a %~1+=1

: start1

echo I am start one !!

rem Other statements... etc

And from there you can have all sorts of functions from start1 to any point you want.

Upvotes: 0

konsolebox
konsolebox

Reputation: 75618

You can also make sure that no part of those sections would execute if %method% doesn't match 1 or 2. You can have goto :eof or just exit with exit /b 1.

if %method%=="1" (goto start1)
if %method%=="2" (goto start2)

echo Invalid method: %method%
goto :eof

:start1
echo start1
pause
exit

:start2
echo start2
pause
exit

Upvotes: 0

Bathsheba
Bathsheba

Reputation: 234875

You have to be careful with whitespace. Write

if "%method%"=="1" (goto start1)

etc instead. You may or may not need the extra quotations around %method%, depending on how you've set up your envrionment variable.

Upvotes: 13

Related Questions