1.

Solve : Batch scripting help?

Answer»

I'm trying to write a batch file that will display a list to the user, have them enter their choice, and then do something. Here's what I have now:

Code: [Select]@echo off
echo 1. BL (main)
echo 2. BL (sound)
echo 3. MS-DOS

choice /C:123 /N
set OS=%errorlevel%

if "%os%"=="1" echo hello1
if "%os%"=="2" echo hello2
if "%os%"=="3" echo hello3
The problem is that nothing is put into os. When I try printing errorlevel after the choice line it doesn't print anything, but shouldn't it be equal to the user's choice? I'm using MS-DOS 6, if it matters.

Thanks!Errorlevel is not set as an Environment Variable but as a Return Code. You must set the Return Code as an Env Var if you wish to test it.

So you could use:
Code: [Select]@echo off
echo 1. BL (main)
echo 2. BL (sound)
echo 3. MS-DOS

choice /C:123 /N
if errorlevel 3 set errorlev=3
if errorlevel 2 set errorlev=2
if errorlevel 1 set errorlev=1

From here on the Return Code is available in the Environment Variable %errorlev%

This is WORTH reading.

Good luck
Ah, OK. Unfortunately, it's still not working - with your code it printing errorlev with 'echo %errorlev%' always prints 1.

Thanks.Quote

echo %errorlev%' always prints 1.

There is a reason for this. Errorlevel 1 means "errorlevel 1 or greater" so the test will always be true, so gotos are needed if 3 or 2 are chosen

@echo off
echo 1. BL (main)
echo 2. BL (sound)
echo 3. MS-DOS

choice /C:123 /N

if errorlevel 3 goto choice3
if errorlevel 2 goto choice2
if errorlevel 1 goto choice1

:choice3

echo you chose 3
goto next

:choice2

echo you chose 2
goto next

:choice1

echo you chose 1

:next


Sorry, I had a brain malfunction...

Reverse the order of the IF statements to be 1 2 3 not 3 2 1

Here's another version with a couple of embellishments:

Code: [Select]@echo off
cls
echo.
echo.
echo 1. BL (main)
echo 2. BL (sound)
echo 3. MS-DOS
echo.
echo.
choice /C:123 /N " Enter your choice..... "
cls

if errorlevel 1 set errorlev=1
if errorlevel 2 set errorlev=2
if errorlevel 3 set errorlev=3

echo.
echo.
echo Return Code = %errorlev%


Hope that fixes it.Great, that worked.

Thanks!You're welcome & again sorry about giving you FALSE INFO to start with.

Good luck


Discussion

No Comment Found