- 1 year ago
how to check if string contain words batch file
To check if a string contains a specific word in a batch file, you can use the FIND
command. Here's an example:
batch@echo off
set "string=This is a sample string"
set "word=sample"
echo %string% | find /i "%word%" > nul
if %errorlevel% equ 0 (
echo The string contains the word.
) else (
echo The string does not contain the word.
)
In this example, we have a variable string
that contains the input string, and a variable word
that contains the word we want to check for. The FIND
command is used to search for the word in the string, and the /i
option makes the search case-insensitive.
The FIND
command's output is redirected to nul
to discard the output, and the %errorlevel%
variable is checked to see if the word was found. If %errorlevel%
is equal to 0, it means the word was found in the string, and the corresponding message is displayed. If %errorlevel%
is not equal to 0, it means the word was not found.
You can modify the string
and word
variables in the example to suit your specific scenario.