Reputation: 53
I am trying to figure out how the "%var:~,1%"
setion of the provided code work.
I thought "%var:~,1%"
would accept the first character correct and ignore everything else after. "%var:~,2%"
would accept only the first two correct characters and so on. In this example "Y" would suffice for "YES". "NO" would suffice for "NOO" and "CLS" would suffice for "CLS"
What happens is only option three "CLS" is a valid option. I can change YES and NOO to "%var:~,3%"
so that they are valid options. But how would have if /I "%var:~,1%" EQU "YES" goto :yes
accept a one character input?
:start
set /p var=is this a yes or no question?
if /I "%var:~,1%" EQU "YES" goto :yes
if /I "%var:~,2%" EQU "NOO" goto :no
if /I "%var:~,3%" EQU "CLS" goto :cls
echo not a valid choice
goto :start
:yes
echo this is YES but you only have to type first letter correct
pause
goto :start
:no
echo this is NO but you have to type the first two letters correct
pause
goto :start
:cls
echo this will CLS but you have to type the first three letters correct
pause
cls
goto :start```
Upvotes: 0
Views: 149
Reputation:
@TripeHound already explained in a comment that you are testing a single character against a word. it should just be if /i "%var:~0,1%"=="y"
.
a much better method however is to use choice
@echo off
:start
choice /C YNC /M "Press Y for Yes, N for No or C for Cancel.
goto :%errorlevel%
:3
echo this will CLS but you have to type the first three letters correct
pause
cls
goto :start
:2
echo this is NO but you have to type the first two letters correct
goto :start
:1
echo this is YES but you only have to type first letter correct
goto :start
If you are determined to not use choice
this will work similarly using set /p
by using only the first character of the word the user inputs.
@echo off
:start
set /p var=is this a yes or no question?
if /i not "%var:~0,1%"=="y" if /i not "%var:~0,1%"=="n" if not "%var:~0,1%"=="c" echo Incorrect choice & goto :start
goto :%var:~0,1%
:c
:C
echo this will CLS but you have to type the first three letters correct
pause
cls
goto :start
:n
:N
echo this is NO but you have to type the first two letters correct
goto :start
:y
:Y
echo this is YES but you only have to type first letter correct
goto :start
Upvotes: 1