我可以在批处理脚本中的一个“查找”命令中搜索多个字符串吗?

问题描述 投票:2回答:3

我有一个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中的位置,当它不是时,它会两次都通过。

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

感谢您的任何帮助!

windows batch-file find findinfiles
3个回答
6
投票

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

0
投票

我尝试了findstr与多个/C:参数(每个一个被搜索的句子),这在我的情况下做了伎俩。所以这是我在一个文件中查找多个句子并重定向输出的解决方案:

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

0
投票

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

: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
© www.soinside.com 2019 - 2024. All rights reserved.