2

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.

4
  • 1
    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 to set "string=%%a". Whilst you're at it, you may as well use set /p "scramble=Y/N: " and set "scramble=Y" too. Commented Mar 10 at 19:11
  • 1
    There's no such command as for /f "text=," and you should decide whether you are working with the variable scramble or with scamble Commented Mar 10 at 19:14
  • 1
    Start reading the help information for every command you are using. Open a Command Prompt window, and type any of e.g. set /?, for /?, if /?, then press the [ENTER] key to read it. Commented Mar 10 at 19:18
  • 2
    Do not use set /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? Commented Mar 11 at 6:55

2 Answers 2

4

This 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!

Sign up to request clarification or add additional context in comments.

Comments

3

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 :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.