Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If a variable equals a number goto

Tags:

batch-file

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

like image 910
Briant Avatar asked Jul 21 '14 07:07

Briant


People also ask

What is == in batch file?

[ == ] (Double Equals) The "IF" command uses this to test if two strings are equal: IF "%1" == "" GOTO HELP. means that if the first parameter on the command line after the batch file name is equal to nothing, that is, if a first parameter is not given, the batch file is to go to the HELP label.

Can you use if statements in batch files?

One of the common uses for the 'if' statement in Batch Script is for checking variables which are set in Batch Script itself. The evaluation of the 'if' statement can be done for both strings and numbers.


1 Answers

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.

like image 150
Bathsheba Avatar answered Oct 19 '22 13:10

Bathsheba