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

int*_*ntA 2 windows batch-file find findinfiles

我有一个Windows批处理脚本,它将在文件中查找字符串

find /i "WD6"  %Inputpath%file.txt
if %errorlevel% == 0 GOTO somestuff
Run Code Online (Sandbox Code Playgroud)

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

find /i "WD6" %acctg_cyc%  %Inputpath%file.txt
if %errorlevel% == 0 GOTO somestuff
Run Code Online (Sandbox Code Playgroud)

但它似乎忽略了%acctg_cyc%并且只在file.txt中查找"WD6".我试过测试%acctg_cyc%file.txt中的位置,当它不是时,它会两次都通过.

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

感谢您的任何帮助!

Ste*_*han 8

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

findstr 有更多的力量,但你必须小心如何使用它:

findstr "hello world" file.txt 
Run Code Online (Sandbox Code Playgroud)

发现任何线路,包含两种helloworld或两者.

了解findstr /?更多信息.

使用(find或findstr)查找一行中的两个单词:

find "word1" file.txt|find "word2"
Run Code Online (Sandbox Code Playgroud)

找到分散在文件上的两个单词(find或findstr):

find "word1" file.txt && find "word2" file.txt
if %errorlevel%==0 echo file contains both words
Run Code Online (Sandbox Code Playgroud)


Pie*_*mol 7

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

findstr /C:"the first search" /C:" a second search " /C:"and another" sourcefile.txt > results.txt
Run Code Online (Sandbox Code Playgroud)