There was a question asked here with the same title, but I don't understand what it was talking about.
All I'm trying to do is see if the end-user is putting in vaguelly the same text. This will also be used in a following menu. but all I need to know is how to do it.
Image Capture of progam running (Example of Menu)
All I'm trying to see if the string named %scamble%
contains "Yes"
, "Y"
, "No"
, or "N"
without having to use a ton of if statements. Again I have no idea what I'm doing... I learned this all self-taught. So treat me as a curious noob.
like
echo This is a ONE TIME prompt (for this session ONLY).
set /p scramble=Y/N: || set scramble=Y
for /f "text=," %%a in (%scamble%) do (
set string = %%a
if "!string:%substring%=!"=="!string!" (
echo Found!
)
)
Again I must reiterate, that I have no clue what I'm doing, but I think I may somewhat understand something. So I ask you, people of the internet, please help! Thanks.
Share edited 2 days ago blackgreen♦ 45.2k28 gold badges161 silver badges156 bronze badges asked Mar 10 at 18:37 BritishSyndicateBritishSyndicate 255 bronze badges 4 Comments disabled on deleted / locked posts / reviews |2 Answers
Reset to default 4This is the simplest and most direct method:
set "options=|Yes|Y|No|N|"
if "!options:|%string%|=!" neq "%options%" (
echo Any of the options found
) else (
echo The answer is invalid
)
The double replacement "!options:|%string%|=!"
tries to remove the current string answer from the options. If the answer is in options, the result is different, so in this case (neq
) the answer is "Found"...
However, the right way to solve this problem is via the choice
command!
Was the Previous Questions Answer:
if not x%str1:bcd=%==x%str1% echo It contains bcd
That Does work but not in loops unless Called:
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION ENABLEEXTENSIONS
set str1=abcdefg
if "%~1" == "" Call :CheckString
pause
exit
:CheckString
if not x%str1:bcd=%==x%str1% echo It contains bcd
exit /b
And you would still need 2 of them so not much difference, ELSE can be handy.
If "%%a" == "Y" (echo Is Y) ELSE (echo Not Y)
Theres also echoing Strings to Findstr:
echo %String% | findstr /I /R /C:".*Y.*" >nul
if "%errorlevel%" == "0" (echo Found Y or Yes)
echo %String% | findstr /I /R /C:".*N.*" >nul
if "%errorlevel%" == "0" (echo Found N or No)
Personally i would just use a choice cmd:
:: Numbers:
echo Press 1 for blah
echo Press 2 for blah
choice /n /c 12
if "%errorlevel%" == "1" (
echo Do whatever for 1...
)
if "%errorlevel%" == "2" (
echo Do whatever for 2...
)
pause
exit
:: Even better Using Numbers:
echo Press 1 for blah
echo Press 2 for blah
choice /n /c 12
:: Loop from 1, up by 1 at a time, upto 5..
For /L %%i in (1,1,5) Do (
If "%errorlevel%" EQU "%%i" (echo Number %%i: This was Selected by User)
)
pause
exit
:: Letters:
echo Press Y for blah
echo Press N for blah
choice /n /c yn
if "%errorlevel%" == "1" (
echo Do whatever for Y...
)
if "%errorlevel%" == "2" (
echo Do whatever for N...
)
pause
exit
Choice is easy, User presses a Key. No Need to Press Enter.
Here is also a Working Loop Example that checks a Variable/String Using both FindString and Calling CheckString:
Set String=abcyNdefg
for /f "delims=" %%a in ("%String%") do (
echo %String% | findstr /I /R /C:".*Y.*" >nul
if "!errorlevel!" == "0" (echo FINDSTR: Found Y or Yes)
Call :CheckString
)
pause
exit
:CheckString
if not x%String:Y=%==x%String% echo CheckString: It contains Y
if not x%String:N=%==x%String% echo CheckString: It contains N
exit /b
Hope it helps :)
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744829418a4595994.html
set string = %%a
defines a variable with a name ending with a space character, and a value beginning with one too. You should change it toset "string=%%a"
. Whilst you're at it, you may as well useset /p "scramble=Y/N: "
andset "scramble=Y"
too. – Compo Commented Mar 10 at 19:11for /f "text=,"
and you should decide whether you are working with the variablescramble
or withscamble
– Magoo Commented Mar 10 at 19:14set /?
,for /?
,if /?
, then press the[ENTER]
key to read it. – Compo Commented Mar 10 at 19:18set /P
for a Yes/No prompt, use the command CHOICE for such a prompt. See: How can I make an "are you sure" prompt in a Windows batch file? – Mofi Commented Mar 11 at 6:55