English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories
4

say i want a batch file to say hello if y is typed in, but bye if n is typed in. how would i do that?

2007-12-08 15:26:26 · 3 answers · asked by e.thade 1 in Computers & Internet Programming & Design

3 answers

techwench gave a decent answer, but if you enter either a capital Y or N, then the script fails because the environment variable check is case sensitive.

I would alter that batch a little to get around this problem. Remove the check against the environment variable and just have the batch jump to either a "y" or a "n" label based on the user input. A batch label called from a 'goto' command is case insensitive.

Like this,

===================
@echo off
:start
cls

set /p userinp=Yes or No (y/n)?
set userinp=%userinp:~0,1%
goto :%userinp%

goto :start

:y
echo Hello
goto :eof

:n
echo Goodbye

================
Now it doesn't matter what case you use, the batch will correctly interpret "y", "Y", "n" and "N".

I also tightened it up a bit by removing the :end label. A simple "goto :eof" is sufficient to jump out of the batch.

2007-12-11 13:08:46 · answer #1 · answered by Kevin 7 · 4 0

@echo off
:start
cls

set /p userinp=Yes or No (y/n)?
set userinp=%userinp:~0,1%
if "%userinp%"=="y" goto 1
if "%userinp%"=="n" goto 2

goto start

:1
echo Hello
goto end

:2
echo Goodbye

:end

pause

2007-12-09 01:01:18 · answer #2 · answered by TechWench 2 · 0 0

C++ basic programming, i'm not sure exactly how to do it tho...

2007-12-08 23:29:27 · answer #3 · answered by Anonymous · 0 1

fedest.com, questions and answers