views:

483

answers:

3

I have tried with the following, but it just says "& was unexpected at this time."

@echo off
:enter-input
echo Please enter a number between 1 and 15:
echo 1 = Selection one
echo 2 = Selection two
echo 4 = Selection three
echo 8 = Selection four
echo x = Quit

set INPUT=
set /P INPUT=Type number: %=%

if "%INPUT%" == "" goto enter-input
if "%INPUT%" == "x" goto end
if "%INPUT%" == "X" goto end

set /A %INPUT%
if %INPUT% & 1 == 1 echo Selection one
if %INPUT% & 2 == 2 echo Selection two
if %INPUT% & 4 == 4 echo Selection three
if %INPUT% & 8 == 8 echo Selection four

echo Done
:end
+3  A: 

I found a way of doing this.

@echo off
:enter-input
echo Please enter a number between 1 and 15:
echo 1 = Selection one
echo 2 = Selection two
echo 4 = Selection three
echo 8 = Selection four
echo x = Quit

set /P INPUT=Type number:

if "%INPUT%" == "" goto enter-input
if "%INPUT%" == "x" goto end
if "%INPUT%" == "X" goto end

set /A isOne = "(%INPUT% & 1) / 1"
set /A isTwo = "(%INPUT% & 2) / 2"
set /A isThree = "(%INPUT% & 4) / 4"
set /A isFour = "(%INPUT% & 8) / 8"

if %isOne% == 1 echo Selection one
if %isTwo% == 1 echo Selection two
if %isThree% == 1 echo Selection three
if %isFour% == 1 echo Selection four

echo Done
:end
loraderon
If you add a line, `SETLOCAL enableextensions enabledelayedexpansion`, after `@echo off` then the `set INPUT=` line wouldn't be needed. Also, what's the `%=%` in the `set /P` statement doing? Lastly, you may want to remove the spaces from the ` == ` ifs; batch files don't really like those.
Patrick Cuff
A: 
SET LEFT = 1
SET RIGHT = 2
SET /A RESULT = %LEFT% & %RIGHT%

Please escape the ampersand character (&) with a '^' if you try it directly in cmd.exe.

A: 

could you please explain how this logic works?

set /A isOne = "(%INPUT% & 1) / 1" 
set /A isTwo = "(%INPUT% & 2) / 2" 
set /A isThree = "(%INPUT% & 4) / 4" 
set /A isFour = "(%INPUT% & 8) / 8"

Thanking you in advance

Gokul