If the variable is equal to goto

If the variable is equal, for example, 1then goto- start1, BUT if the same variable is equal 2, then goto- start2.

This is what I still have:

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

:start1
echo start1
pause
exit

:start2
echo start2
pause
exit

But even if the variable methodis equal 2, it always echoes me start1...

+4
source share
3 answers

You must be careful with spaces. To write

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

etc. You may or may not need additional suggestions around %method%, depending on how you set up the envrionment variable.

+4
source

, , %method% 1 2. goto :eof 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
0

Having many if statements is not very accurate .. there is a counter for your if statement comparison. That should do the trick, I think ...

@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.

0
source

All Articles