批处理文件:查找子字符串是否在字符串中(不在文件中)

272
在批处理文件中,我有一个字符串abcdefg。 我想检查字符串中是否存在bcd
不幸的是,似乎我找到的所有解决方案都是在文件中搜索子字符串,而不是在字符串中搜索子字符串。
是否有简便的方法来解决这个问题?

7
顺便说一下,通常是使用“Windows”和“cmd”,或者使用“ms-dos”。很长一段时间以来,MSDOS不再是Windows的一部分。 - paxdiablo
13个回答

1
更好的答案在这里
set "i=hello " world"
set i|find """" >nul && echo contains || echo not_contains

在你提供的问题中,使用SET解决了一个特定且独特的需求,因此提到“SET”非常重要。然而,在当前讨论中似乎没有增加任何内容,因为已经提供了将命令管道传递给FIND命令并测试结果的方法。 - Ben Personick

1

@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
)

1

是的,我们可以在字符串中找到子字符串:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接