简体   繁体   中英

Check a string for a substring in a batch file (Windows)?

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?

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.

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
)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM