Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking input via a batch file

Tags:

batch-file

I am doing user input and checking to see if they typed n or y... and it's not working because it says both either way.

Here's what I have:

@echo off
set /P theuserinput="Type your name: "
echo So your name is: %theuserinput%?
set /P isit="Y/N: "
echo You typed: %isit%
if (%isit% == "y") goto :saidyes
if (%isit% == "n") goto :saidno

:saidyes
echo Hooray!

:saidno
echo Aww
PAUSE
like image 773
test Avatar asked Sep 23 '26 00:09

test


2 Answers

First you can add a default goto after the two if's.

Then, in both tests you have to add the quotes around %isit% and to remove the parenthesis. You may also add the /I flag to do an insensitive string comparison.

Finally, add goto after each echo to jump over the next one.

@echo off
set /P theuserinput="Type your name: "
echo So your name is: %theuserinput%?
set /P isit="Y/N: "
echo You typed: %isit%
if /I "%isit%" == "Y" goto :saidyes
if /I "%isit%" == "N" goto :saidno
goto :error

:saidyes
echo Hooray!
goto :end

:saidno
echo Aww
goto :end

:error
echo ERROR

:end
PAUSE
like image 161
cedrou Avatar answered Sep 26 '26 21:09

cedrou


Need change in syntax

Here is the modified code

    @echo off
set /P theuserinput="Type your name: "
echo So your name is: %theuserinput%?
set /P isit="Y/N: "
echo You typed: %isit%
if "%isit%" == "Y" GOTO saidyes
if "%isit%" == "N" GOTO saidno

:saidyes
echo Hooray!
GOTO paused

:saidno
echo Aww

:paused
PAUSE

....

In above example The Y/N is supposed to be capital letter only.

like image 28
rajesh Avatar answered Sep 26 '26 21:09

rajesh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!