Testing on input
Moderator: DosItHelp
Testing on input
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.
Thank you.
Re: Testing on input
What operating system? If you are using Vista, 7 or 8 then just use the Choice command.
Re: Testing on input
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)
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)
-
- Posts: 257
- Joined: 04 Mar 2014 11:14
- Location: germany
Re: Testing on input
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"
Phil
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.
Re: Testing on input
Fragile wrote:I'm using 7
Any exampes of the choice command?
Why not open up a cmd prompt an type: choice /?
Re: Testing on input
pieh-ejdsch wrote:with using only Set /p you put them into a for /f Loop:
Phil



Re: Testing on input
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
Re: Testing on input
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
Re: Testing on input
Why bother using the code to upper case the letter. You can do a case insensitive match with the IF command.
Re: Testing on input
Here is another approach.The end user must choose the correct option; (or close the console window).
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
Re: Testing on input
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
Re: Testing on input
Actually it is possible to executed the input as code:
In addition i'm a friend of "exact" solutions, so Nooo, yup, ... are not ignored but handled as wrong input:
penpen
If you want to avoid this, you should use delayed expansion.Z:\>inputTest.bat
Yes or No? a" == "a" (echo Hello world!) else if "a
Hello world!
Z:>
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
Re: Testing on input
And this a version without any goto:
penpen
Edit: Removed a bug, that sometimes results in an endless loop.
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.
-
- Posts: 257
- Joined: 04 Mar 2014 11:14
- Location: germany
Re: Testing on input
another Version
Phil
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