SoFunction
Updated on 2025-04-08

How to determine if the parameters contain certain strings in batch processing?

How to determine if the parameters contain certain strings in batch processing?

Generally, when judging the exact match, use ==

Copy the codeThe code is as follows:

@echo off
if "%1"=="abc"  @echo OK

But how to write partial matches? For example, when determining that the first three characters in the parameter are "abc"

Copy the codeThe code is as follows:

@echo off
echo %1|findstr "^abc" >nul
echo %errorlevel%

The test results are as follows:

C:\>test xixi
1

C:\>test
1

C:\>test abcd
0

C:\>test xabcd
1

Thank you very much, but how can I achieve the effect of if else?

for example

if "%1" contains "abc" @echo OK
if "%1" contains "def" @echo NG

Copy the codeThe code is as follows:

@echo off
echo %1|findstr "^abc" >nul
if %errorlevel% equ 0 (
echo ok
) else (
echo not ok
)