我可以在批处理脚本中的一个 "find" 命令中搜索多个字符串吗?

Can I search for multiple strings in one "find" command in batch script?

我有一个 windows 批处理脚本,可以在文件中查找字符串

find /i "WD6"  %Inputpath%file.txt
if %errorlevel% == 0 GOTO somestuff

目前我的代码是这样的。我遇到了一个新字符串,我想在同一个文件中搜索并在找到它时执行相同的操作,它存储在一个名为 %acctg_cyc% 的变量中我可以在一行代码中搜索这两个字符串吗?我试过这个:

find /i "WD6" %acctg_cyc%  %Inputpath%file.txt
if %errorlevel% == 0 GOTO somestuff

但它似乎忽略了 %acctg_cyc%,只在 file.txt 中寻找 "WD6"。我尝试测试 %acctg_cyc% 在 file.txt 中的位置和不在的位置,并且两次都通过了。

有什么想法吗?我知道我可以用更多的代码行来做到这一点,但我现在真的在努力避免这种情况。也许这是不可能的。

感谢您的帮助!

find 不是很强大。它只搜索一个字符串(即使它是两个单词):find "my string" file.txt 查找字符串 my string.

findstr更强大,但你必须小心如何使用它:

findstr "hello world" file.txt 

找到任何包含 helloworld 或两者的行。

有关详细信息,请参阅 findstr /?

使用(find 或 findstr)可以在一行中找到两个词:

find "word1" file.txt|find "word2"

找到散布在文件中的两个词(find 或 findstr):

find "word1" file.txt && find "word2" file.txt
if %errorlevel%==0 echo file contains both words

我尝试 findstr 使用多个 /C: 参数(每个要搜索的句子一个),这在我的案例中起到了作用。 所以这是我在一个文件中查找多个句子并重定向输出的解决方案:

findstr /C:"the first search" /C:" a second search " /C:"and another" sourcefile.txt > results.txt

我用过这个。也许不是很正统,但有效! 它等到浏览器关闭

:do_while_loop
rem ECHO LOOP %result%
rem pause
tasklist /NH | find "iexplore.exe"
set result=%ERRORLEVEL%
tasklist /NH | find "firefox.exe"
set result=%result%%ERRORLEVEL%
if not "%result%"=="11" goto :do_while_loop