如何检查文本文件中是否批量包含某些文本? [英] How to check if text file contain certain text in batch?

查看:241
本文介绍了如何检查文本文件中是否批量包含某些文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的文本文件与批处理文件所在的文件夹相同.

I have text file in same folder as where my batch file is.

所以我希望我的批处理文件读取文本文件的内容,并且根据该文本文件的内容,我希望它执行操作.

so I want my batch file to read content of the text file and depending on content of that text file I want it to perform action.

例如,如果文本文件包含"Hello World",则类似于开始VLC" 如果其中不包含Hello World,请执行其他操作.

example if text file contains "Hello World" then do like Start VLC if it doesn't contain Hello World then do something else.

文本将自行更新.

到目前为止,这是我的批处理代码,它可以从屏幕上的文本文件输出文本.

here is my batch code so far, it can output the text from text file on screen.

@echo off
for /f "tokens=*" %%a in (log.txt) do call :processline %%a

pause
goto :eof

:processline
echo line=%*

goto :eof

:eof

推荐答案

您应该使用FIND或FINDSTR.在命令提示符下键入HELP FINDHELP FINDSTR以获取文档. FIND非常简单可靠. FINDSTR功能强大得多,但也很有气质.有关更多信息,请参见 Windows FINDSTR命令的未记录功能和限制是什么?.

You should use either FIND or FINDSTR. Type HELP FIND and HELP FINDSTR from a command prompt to get documentation. FIND is very simple and reliable. FINDSTR is much more powerful, but also tempermental. See What are the undocumented features and limitations of the Windows FINDSTR command? for more info.

您不必关心这两个命令的输出,因此可以将输出重定向到nul.

You don't care about the output of either command, so you can redirect output to nul.

如果找到该字符串,两个命令都将ERRORLEVEL设置为0,如果找不到该字符串,则将其设置为1.您可以使用&&||运算符根据是否找到字符串来有条件地执行代码.

Both commands set ERRORLEVEL to 0 if the string is found, and 1 if the string is not found. You can use the && and || operators to conditionally execute code depending on whether the string was found.

>nul find "Hello World" log.txt && (
  echo "Hello World" was found.
) || (
  echo "Hello World" was NOT found.
)

>nul findstr /c:"Hello World" log.txt && (
  echo "Hello World" was found.
) || (
  echo "Hello World" was NOT found.
)

您也可以在IF语句中测试ERRORLEVEL,但我更喜欢上面的语法.

You could also test ERRORLEVEL in an IF statement, but I prefer the syntax above.

这篇关于如何检查文本文件中是否批量包含某些文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆