windows 我可以在批处理脚本的一个“查找”命令中搜索多个字符串吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31518737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I search for multiple strings in one "find" command in batch script?
提问by intA
I have a windows batch script that will look for a string within a file
我有一个 Windows 批处理脚本,它将在文件中查找字符串
find /i "WD6" %Inputpath%file.txt
if %errorlevel% == 0 GOTO somestuff
Currently this is what my code looks like. I've come across a new string I want to search for in the same file and do the same action if it finds it, it stored it in a variable called %acctg_cyc%
can I search for both strings in one line of code? I tried this:
目前这就是我的代码的样子。我遇到了一个我想在同一个文件中搜索的新字符串,如果找到它就执行相同的操作,它将它存储在一个名为的变量%acctg_cyc%
中,我可以在一行代码中搜索两个字符串吗?我试过这个:
find /i "WD6" %acctg_cyc% %Inputpath%file.txt
if %errorlevel% == 0 GOTO somestuff
But it seems to ignore the %acctg_cyc% and only look for "WD6" in file.txt. I tried testing where %acctg_cyc%
is in file.txt and when it is not and it passes both times.
但它似乎忽略了 %acctg_cyc% 而只在 file.txt 中查找“WD6”。我尝试测试%acctg_cyc%
file.txt 中的位置,何时不在,并且两次都通过。
Any thoughts? I know I could do this in more lines of code but I'm really trying to avoid that right now. Maybe it's just not possible.
有什么想法吗?我知道我可以用更多的代码行来做到这一点,但我现在真的想避免这种情况。也许只是不可能。
Thank you for any help!
感谢您的任何帮助!
回答by Stephan
find
isn't very powerful. It searches for one string only (even if it is two words): find "my string" file.txt
looks for the string my string
.
find
不是很强大。它只搜索一个字符串(即使是两个词):find "my string" file.txt
查找字符串my string
。
findstr
has much more power, but you have to be careful how to use it:
findstr
有更多的权力,但你必须小心如何使用它:
findstr "hello world" file.txt
finds any line, that contains either hello
or world
or both of them.
发现任何线路,包含两种hello
或world
或两者。
see findstr /?
for more info.
查看findstr /?
更多信息。
Finding both words in one line is possible with (find or findstr):
可以使用(find 或 findstr)在一行中查找两个单词:
find "word1" file.txt|find "word2"
finding both words scattered over the file (find or findstr):
查找分散在文件中的两个单词(find 或 findstr):
find "word1" file.txt && find "word2" file.txt
if %errorlevel%==0 echo file contains both words
回答by Piemol
I tried findstr
with multiple /C:
arguments (one for each to be searched sentence) which did the trick in my case.
So this is my solution for finding multiple sentences in one file and redirect the output:
我尝试findstr
了多个/C:
参数(每个要搜索的句子一个),这在我的案例中起到了作用。所以这是我在一个文件中查找多个句子并重定向输出的解决方案:
findstr /C:"the first search" /C:" a second search " /C:"and another" sourcefile.txt > results.txt
回答by user2928048
I used this. Maybe not much orthodox, but works! It waits until browsers dismiss
我用过这个。也许没有太多正统,但有效!它等到浏览器关闭
: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