50

I wrote a simple batch file to run Frequently Used websites based on a number selection. Here's the code I have. I am trying to set it so if someone inputs a number 6 or greater it will go to :N but whenever I type 6 the batch file exits. I have tried if %input% > 6 goto :N but it just tells me I am going to Google.

@echo off
:Start2 
cls
goto Start
:Start
title Frequently Used Websites
echo Please select a website from the list
echo with the corresponding key
echo --------------------------------------
echo [1] Google
echo [2] Wikipedia
echo [3] Facebook
echo [4] Youtube
echo [5] Yahoo
set input=
set /p input= Choice:
if %input%==1 goto Z if NOT goto Start2
if %input%==2 goto X if NOT goto Start2
if %input%==3 goto C if NOT goto Start2
if %input%==4 goto V if NOT goto Start2
if %input%==5 goto B if NOT goto Start2
if %input%>=6 goto N

:Z
cls
echo You have selected Google
pause
start www.google.com
exit
:X
cls
echo You have selected Wikipedia
pause
start www.wikipedia.com
exit
:C
cls
echo You have selected Facebook
pause
start www.facebook.com
exit
:V
cls
echo You have selected Youtube
pause
start www.youtube.com
exit
:B
cls
echo You have selected Yahoo
pause
start www.Yahoo.com
exit
:N
cls
echo Invalid Selection! Try again
pause
goto :start2
Ross Ridge
  • 38,414
  • 7
  • 81
  • 112
gm95
  • 802
  • 2
  • 10
  • 16
  • 2
    Since the only time it will ever arrive on the line `if %input%>=6 goto N` is when it's >=6, you could just drop the if, and write `goto N`. – Kitsune Feb 14 '12 at 14:43
  • 3
    Try `if /?` and it would help you – jeb Feb 14 '12 at 15:11

4 Answers4

118

try this:

if 3 gtr 2 @echo "biggger"

This outputs:

"biggger"

enter image description here

The other operators are:

EQU - equal
NEQ - not equal
LSS - less than
LEQ - less than or equal
GTR - greater than
GEQ - greater than or equal

Reference

Community
  • 1
  • 1
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
9
    if %var% geq 1

is the easiest way

  • 10
    Two year old question with good accepted answer. Try to make answer which really adds something new, with more content. – manuell Feb 22 '14 at 12:16
  • 1
    Actually this answer is a bit different. Accepted answer compares two constants. This one at least shows a variable use. For those who can not differentiate between `var` and `%var%` it makes a difference. – MelBurslan Oct 24 '21 at 01:53
1

Actually, you don't even need a greater feature. All you need to do is add

goto homepagename

Then you will be taken there if none of the if commands execute a goto command.

For example, this will fix your code:

@echo off
:Start2 
cls
goto Start
:Start
title Frequently Used Websites
echo Please select a website from the list
echo with the corresponding key
echo --------------------------------------
echo [1] Google
echo [2] Wikipedia
echo [3] Facebook
echo [4] Youtube
echo [5] Yahoo
set input=
set /p input= Choice:
if %input%==1 goto Z if NOT goto Start2
if %input%==2 goto X if NOT goto Start2
if %input%==3 goto C if NOT goto Start2
if %input%==4 goto V if NOT goto Start2
if %input%==5 goto B if NOT goto Start2
if %input%>=6 goto N
goto Start
DJMcMayhem
  • 7,285
  • 4
  • 41
  • 61
user3674709
  • 133
  • 1
  • 2
0

You can write this (easier)

@echo off

:Start2
cls
goto Start

:Start
title Frequently Used Websites
echo Please select a website from the list
echo with the corresponding key
echo --------------------------------------
echo [1] Google
echo [2] Wikipedia
echo [3] Facebook
echo [4] Youtube
echo [5] Yahoo

set /p input= Choice: 

if %input%==1 goto Z
if %input%==2 goto X
if %input%==3 goto C
if %input%==4 goto V
if %input%==5 goto B
echo Invalid selection!
echo.
echo Press any key to go back!
pause >nul
cls
goto start2

:Z
cls
echo You have selected Google
pause
start www.google.com
exit

:X
cls
echo You have selected Wikipedia
pause
start www.wikipedia.com
exit

:C
cls
echo You have selected Facebook
pause
start www.facebook.com
exit

:V
cls
echo You have selected Youtube
pause
start www.youtube.com
exit

:B
cls
echo You have selected Yahoo
pause
start www.Yahoo.com
exit

:N
cls
echo Invalid Selection! Try again
pause
goto start2
kokbira
  • 608
  • 1
  • 10
  • 25
  • @TLama, as far as I was aware, [this](http://stackoverflow.com/revisions/bdc9691d-6963-4b70-9db2-2b8e582a3e91/view-source) was his posting, which appeared to be new lines during the review (perhaps my suggestion should have been rejected). Regardless, the answer needs formatting. If it's a one line statement, it should be one line, not a large paragraph. – matth Nov 25 '13 at 03:07