21

Let's say I have some text in a variable called $1. Now I want to check if that $1 contains a certain string. If it contains a certain string I want to print a message. The printing is not the problem, the problem is the check. Any ideas how to do that?

John Smith
  • 7,243
  • 6
  • 49
  • 61
EOB
  • 2,975
  • 17
  • 43
  • 70

3 Answers3

24

The easiest way in my opinion is this :

set YourString=This is a test

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

Basiclly the string after ':' is the string you are looking for and you are using not infront of the if because %string:*% will remove the * from the string making them not equal.

Wald
  • 1,063
  • 7
  • 13
  • 4
    As with [Igor's answer](http://stackoverflow.com/a/8757658/1012053), this test ignores case. Also, it is safer to use delayed expansion. Finally, it cannot be used if the substring contains `=`. – dbenham Jul 30 '15 at 14:52
  • 1
    I want to provide an example what is happening here. If you have a string `abc` and you want to check for a substring `b` the expression is `not "abc"=="abc:b"` and it's evaluated as `not "abc"=="ac"` (true). If we search for `x` in the same string we have `not "abc"=="abc"`(false) because `abc:x` won't replace any symbol. – Dr.eel Apr 05 '18 at 07:21
11

The SET search and replace trick works in many cases, but it does not support case sensitive or regular expression searches.

If you need a case sensitive search or limited regular expression support, you can use FINDSTR.

To avoid complications of escaping special characters, it is best if the search string is in a variable and both search and target are accessed via delayed expansion.

You can pipe $1 into the FINDSTR command with the ECHO command. Use ECHO( in case $1 is undefined, and be careful not to add extra spaces. ECHO !$1! will echo ECHO is off. (or on) if $1 is undefined, whereas ECHO(!$1! will echo a blank line if undefined.

FINDSTR will echo $1 if it finds the search string - you don't want that so you redirect output to nul. FINDSTR sets ERRORLEVEL to 0 if the search string is found, and 1 if it is not found. That is what is used to check if the string was found. The && and || is a convenient syntax to use to test for match (ERRORLEVEL 0) or no match (ERRORLEVEL not 0)

The regular expression support is rudimentary, but still useful.

See FINDSTR /? for more info.

This regular expression example will search $1 for "BEGIN" at start of string, "MID" anywhere in middle, and "END" at end. The search is case sensitive by default.

set "search=^BEGIN.*MID.*END$"
setlocal enableDelayedExpansion
echo(!$1!|findstr /r /c:"!search!" >nul && (
  echo FOUND
  rem any commands can go here
) || (
  echo NOT FOUND
  rem any commands can go here
)
dbenham
  • 127,446
  • 28
  • 251
  • 390
  • This answer helped me a lot. Although I wish that it was a bit organized of what does what. Know you explained it in but hard to understand. – Jonathan J. Pecany Jun 13 '20 at 22:08
2

As far as I know cmd.exe has no built-in function which answers your question directly. But it does support replace operation. So the trick is: in your $1 replace the substring you need to test the presence of with an empty string, then check if $1 has changed. If it has then it did contain the substring (otherwise the replace operation would have had nothing to replace in the first place!). See the code below:

set longString=the variable contating (or not containing) some text

@rem replace xxxxxx with the string you are looking for
set tempStr=%longString:xxxxxx=%

if "%longString%"=="%tempStr%" goto notFound
echo Substring found!
goto end

:notFound
echo Substring not found

:end
Igor Korkhov
  • 8,283
  • 1
  • 26
  • 31
  • 1
    Note - this type of search is case insensitive. Also can have problems with special characters. It is more robust when using delayed expansion for both the search/replace and the equality check. But search string cannot contain `=` char, and search string cannot start with `~` or `*`. – dbenham Jan 06 '12 at 13:02
  • @dbenham: Absolutely fair remark, my solution is far from perfect (as is cmd syntax, though). – Igor Korkhov Jan 06 '12 at 13:16