Replacement for choice.com on Windows 7 x64

I have a plenty of Windows interactive batch files that are using choice.com tool as a helper when I need some input from the user, for example:

:: ***********
:: * BEGIN   *
:: *********** 
:begin
color 0e
cls
echo.
echo **********************************************
echo You started interactive batch script.... 
echo **********************************************
echo Select server:
echo (1) EUROPE 
echo (2) ASIA
echo (3) US
echo (4) ALL THREE
echo (5) EXIT 
echo.

..\util\choice /C:12345 Pick one

if errorlevel 5 goto end
if errorlevel 4 goto all
if errorlevel 3 goto US
if errorlevel 2 goto ASIA
if errorlevel 1 goto EUROPE

:EUROPE
... do some stuff ...
goto end

:ASIA
... do some stuff ...
goto end

:US
... do some stuff ...
goto end

:ALL
... do some stuff ...
goto end

:: END
:END

Choice.com is 16-bit application that can not run on Windows x64. The simplest workaround that I found is to use SET /P command. It’s a little bit of more code to write, but it’s quite trivial and does not hurt script readability. We can rewrite above script as:

:: ***********
:: * BEGIN   *
:: *********** 
:begin
color 0e
cls
echo.
echo **********************************************
echo You started interactive batch script.... 
echo **********************************************
echo Select server:
echo (1) EUROPE 
echo (2) ASIA
echo (3) US
echo (4) ALL THREE
echo (5) EXIT 
echo.

set choice=
set /P choice="Select 1..5: "

::
:: I used ~0,1 to "substring" first character from the input
::
if not '%choice%'=='' set choice=%choice:~0,1%

if /I '%choice%'=='1' goto europe
if /I '%choice%'=='2' goto asia
if /I '%choice%'=='3' goto all
if /I '%choice%'=='5' goto end

::
:: if we came here then we know that user entered 
::
echo "%choice%" is not a valid option
pause
echo.
goto begin


:EUROPE
... do some stuff ...
goto end

:ASIA
... do some stuff ...
goto end

:US
... do some stuff ...
goto end

:ALL
... do some stuff ...
goto end

:: END
:END

Posted on 08.11.2012, in MS Windows and tagged , . Bookmark the permalink. Comments Off on Replacement for choice.com on Windows 7 x64.

Comments are closed.