Sometimes, we need to find a substring exists in a string. For example , string contains ‘hello world’ and we need to check if substring ‘hello’ exists in the string.

DOS provides conditional statements (if else), allowing you to execute commands based on conditional values.

The exist command is used to check if a file or directory exists in a given directory. For example, the exist file command checks if a file exists in the current directory.

There are multiple ways which we can find substring exists in a string

using findStr command

This tutorial checks an word found in a string or not.

findStr is a similar to find to search a word in a another string . But findStr supports more complex regular expressions than find and mostly used in searching content in file(s)

@echo off
set str=Hello World.
echo %str% | findstr /I "%1" > nul

if errorlevel 0 (
    echo word found

) else (
    echo word not found
)

Output:

E:\dos>test.bat world
0
Substring found

E:\dos>test.bat world1
1
Substring not found

E:\dos>test.bat World1
1
Substring not found

using find command

Using

@echo off
set str=Hello World.
set word=%1

if "%str:%word=%"=="%word%" (
    echo word not found
) else (
    echo word found
)