在批处理文件中,我有一个字符串abcdefg。我想检查bcd是否在字符串中。

不幸的是,似乎所有的解决方案,我找到搜索一个文件的子字符串,而不是一个字符串的子字符串。

有没有简单的解决方法?


当前回答

建立在@user839791的答案上,但我又添加了一些东西。

@echo off
rem --Set variable below--
set var=condition

rem --Uncomment below line to display contents of variable--
::echo The variable is %var%

rem --Change condition to desired string below--
ECHO.%var%| FIND /I "condition">Nul && (  
  rem --Occurs if the string is found--
  Echo.Variable is "condition"
  color C
  pause
) || (
  rem --Occurs if the string isn't found--
  Echo.Variable is not "condition"
  color A
  pause
)

其他回答

ECHO %String%| FINDSTR /C:"%Substring%" && (Instructions)

建立在@user839791的答案上,但我又添加了一些东西。

@echo off
rem --Set variable below--
set var=condition

rem --Uncomment below line to display contents of variable--
::echo The variable is %var%

rem --Change condition to desired string below--
ECHO.%var%| FIND /I "condition">Nul && (  
  rem --Occurs if the string is found--
  Echo.Variable is "condition"
  color C
  pause
) || (
  rem --Occurs if the string isn't found--
  Echo.Variable is not "condition"
  color A
  pause
)

是的,我们可以在String中找到subString:

echo.%data% | FINDSTR /I "POS">Nul && (SET var=POS) || (SET noVar="variable not found")

echo.%data% | FINDSTR /I "TD1">Nul && (SET var=TD1) || (SET noVar="variable not found")

GOTO %var%

:POS   
echo processes inside POS  
GOTO END

:TD1   
echo processes inside TD1

:END

对我来说最好的方法是:

call :findInString "HelloWorld" "World"

:findInString
ECHO.%1 | FIND /I %2>Nul && ( 
  Echo.Found
) || (
  Echo.Not found
)

在Var中查找文本,示例:

var_text="demo string test"
Echo.%var_text% | findstr /C:"test">nul && (
    echo "found test" 
    ) || Echo.%var_text% | findstr /C:"String">nul && (
             echo "found String with S uppercase letter" 
    ) || (
             echo "Not Found " 
    )

传说:

& Execute_that和execute_this || Ex: Execute_that IF_FAIL execute this && Ex: Execute_that IF_SUCCESSFUL执行此操作 >nul命令无回显结果 中 /C:使用字符串作为文字搜索字符串