1

I'm following the answer in Check a string for a substring in a batch file (Windows)?. Wald's answer,

set YourString=This is a test

If NOT "%YourString%"=="%YourString:test=%" (
    echo Yes
) else (
    echo No
)

does kinda work. However, I'm having trouble changing test= into a variable of my own. I followed another answer of concatenating 2 variables but none of them seemed to work. Basically, I want to check if 'YourString' contains 'OtherString' without a super long amount of code.

Here is the code I have right now

set YourString=This is a test
set OtherString=test

If NOT "%YourString%"=="%YourString:%%=%" (
    echo Yes
echo !YourString%OtherString%=!
pause
) else (
    echo No
echo "%YourString:test=%"
pause
)
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Andly Kwan
  • 61
  • 1
  • 7
  • 1
    Interesting that you know to use delayed expansion on the `echo !YourString%OtherString%=!` line (that I suspect is supposed to be `echo !YourString:%OtherString%=!` but you aren't using it in the `if` statement as `!YourString:%OtherString%=!` – SomethingDark Jun 13 '22 at 18:11

0 Answers0