Testing on input

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Fragile
Posts: 2
Joined: 19 Mar 2014 14:37

Testing on input

#1 Post by Fragile » 19 Mar 2014 14:40

I want to make input to check only on Y/Yes or N/No not on spaces abcdef etc. How can I do that?
Thank you.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Testing on input

#2 Post by Squashman » 19 Mar 2014 17:24

What operating system? If you are using Vista, 7 or 8 then just use the Choice command.

Fragile
Posts: 2
Joined: 19 Mar 2014 14:37

Re: Testing on input

#3 Post by Fragile » 20 Mar 2014 10:50

I'm using 7

I only got like:
:nospace
SET /P ANSWER=Do you want to clean your mw2 folder?(Y/N)
IF NOT DEFINED ANSWER GOTO :nospace
echo You chose: %ANSWER%
if /i {%ANSWER%}=={y} (goto :yes)
if /i {%ANSWER%}=={yes} (goto :yes)
goto :no
exit /b 0

But it crashes when space then enter OR on any other letter besides Y/Yes N/NO
Any exampes of the choice command?
(I know you dont need the :nospace I did it just for the fun)

pieh-ejdsch
Posts: 257
Joined: 04 Mar 2014 11:14
Location: germany

Re: Testing on input

#4 Post by pieh-ejdsch » 20 Mar 2014 22:25

good morning,

with using only Set /p you put them into a for /f Loop:
you Do not need to check if n/no into for-Loop: then into for-Loop at last "Else goto :false"

Code: Select all

@echo off

:Input
for /f delims^= %%i in ('^>con set /p "X=Shuld it be ? give me [Y], [Yes] or [N], [No]: " ^&^&cmd /von/c echo(!X!'
 ) do   if /i %%i equ Y   (goto :true
 ) else if /i %%i equ yes (goto :true
 ) else if /i %%i equ N   (goto :false
 ) else if /i %%i equ No   goto :false

echo  not for choice ... again!
goto :Input

:true
echo You do it
pause
exit /b 0

:false
echo You do NOT
pause
exit /b 1


Phil
Last edited by pieh-ejdsch on 21 Mar 2014 02:26, edited 1 time in total.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Testing on input

#5 Post by Squashman » 20 Mar 2014 22:32

Fragile wrote:I'm using 7
Any exampes of the choice command?

Why not open up a cmd prompt an type: choice /?

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Testing on input

#6 Post by Squashman » 20 Mar 2014 22:38

pieh-ejdsch wrote:with using only Set /p you put them into a for /f Loop:
Phil

:?: :?: :?:

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Testing on input

#7 Post by foxidrive » 21 Mar 2014 00:50

pieh-ejdsch wrote:with using only Set /p you put them into a for /f Loop:

Code: Select all

@echo off

:Input
for /f delims^= %%i in ('set /p "X=Shuld it be ? give me [Y], [Yes] or [N], [No]: "^>con ^&cmd /von/c echo !X!'
 ) do   if /i %%i equ Y   (goto :true
 ) else if /i %%i equ yes (goto :true
 ) else if /i %%i equ N   (goto :false
 ) else if /i %%i equ No   goto :false

echo  not for choice ... again!
goto :Input

:true
echo You do it
pause
exit /b 0

:false
echo You do NOT
pause
exit /b 1


Phil



Phil, there is no need to use a for loop:

Code: Select all

@echo off

:Input
set /p "X=Shuld it be ? give me [Y], [Yes] or [N], [No]: "
   if /i "%x%" equ "Y"    goto :true
   if /i "%x%" equ "yes"  goto :true
   if /i "%x%" equ "N"    goto :false
   if /i "%x%" equ "No"   goto :false

echo  not for choice ... again!
goto :Input

:true
echo You do it
pause
exit /b 0

:false
echo You do NOT
pause
exit /b 1

miskox
Posts: 668
Joined: 28 Jun 2010 03:46

Re: Testing on input

#8 Post by miskox » 21 Mar 2014 10:37

Code: Select all

@echo off

set yesno=&rem

set /p "yesno=Yes or No? "

REM If user pressed ENTER only then answer is X

if "%yesno%"=="" set yesno=X

REM Remove spaces

set yesno=%yesno: =%

REM After removing all spaces ENTER only can remain, so again yesno=X

if "%yesno%"=="" set yesno=X

REM Small n to upper N
REM Small y to upper Y

set yesno=%yesno:n=N%
set yesno=%yesno:y=Y%

REM Only first character is what we need. (regardless of No, Nooooooo...)

set yesno=%yesno:~0,1%

REM and now test it

if "%yesno%"=="Y" echo YES
if "%yesno%"=="N" echo NO


I added option X if somebody needs it. Of course this could trigger goto back to the prompt immediately.

I use similar code just to enter N (for NO) and everything else is treated as YES.

Saso

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Testing on input

#9 Post by Squashman » 21 Mar 2014 11:32

Why bother using the code to upper case the letter. You can do a case insensitive match with the IF command.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Testing on input

#10 Post by Compo » 21 Mar 2014 13:08

Here is another approach.

Code: Select all

@Echo off&SetLocal
:Loop
Set "ANS="
Echo(&Set/p "ANS= Would you like to continue? [Y]es/[N]o "
Echo([%ANS%]|FindStr/i /c:"[y]" /c:"[yes]" /c:"[n]" /c:"[no]">Nul||GoTo :Loop
Echo(&Echo( You selected %ANS%
Ping -n 4 127.0.0.1 1>Nul
The end user must choose the correct option; (or close the console window).

miskox
Posts: 668
Joined: 28 Jun 2010 03:46

Re: Testing on input

#11 Post by miskox » 21 Mar 2014 14:14

Squashman wrote:Why bother using the code to upper case the letter. You can do a case insensitive match with the IF command.


Good catch. Sometimes I just miss some things (forget).

Saso

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Testing on input

#12 Post by penpen » 22 Mar 2014 04:41

Actually it is possible to executed the input as code:
Z:\>inputTest.bat
Yes or No? a" == "a" (echo Hello world!) else if "a
Hello world!

Z:>
If you want to avoid this, you should use delayed expansion.
In addition i'm a friend of "exact" solutions, so Nooo, yup, ... are not ignored but handled as wrong input:

Code: Select all

@echo off
setlocal enableDelayedExpansion
:input
set "input="
set /P "input=Question (Y[es]]|[N[o])? "
if /I "!input:Yes=Y!" == "Y" (
   echo(found: yes
) else if /I "!input:No=N!" == "N" (
   echo(found: no
) else goto :input
endlocal
goto :eof

penpen

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Testing on input

#13 Post by penpen » 22 Mar 2014 08:49

And this a version without any goto:

Code: Select all

@echo off
setlocal
set "C=(if not defined text set "text=Default question text (Y[es]] / [N[o])? " ) & (set "input=" & set /P "input=!text!") & if /I "!input:Yes=Y!" == "Y" (exit /B 1) else if /I "!input:No=N!" == "N" (exit /B 0) else cmd /V:ON /C "%%C%%""
set "CHOICE=cmd /V:ON /C "%%C%%""

set "text=Question text (Y[es]] / [N[o])? "
%CHOICE% && echo no || echo yes

endlocal

penpen

Edit: Removed a bug, that sometimes results in an endless loop.

pieh-ejdsch
Posts: 257
Joined: 04 Mar 2014 11:14
Location: germany

Re: Testing on input

#14 Post by pieh-ejdsch » 23 Mar 2014 10:41

another Version

Code: Select all

@echo off
for /f "eol=0 tokens=2,3 delims=(/)" %%a  in ('"echo n|xcopy /L/-y %windir%\win.ini %windir%\system.ini"') do (
 <nul set/p=Choice %%a %%b ?
 xcopy /Lpy "%~f0" "%~dp0.xc)*"|findstr /b [1-9] >nul &&echo %%a ||echo %%b
)

pause
exit /b


Phil

Post Reply