stringif-statementbatch-filecmdsubstring

How to check if a string contains a substring in batch


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.


Solution

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