如何使用 Windows 命令行查找文件是否包含给定字符串
我正在尝试为 Windows XP 创建一个批处理 (.bat) 文件来执行以下操作:
If (file.txt contains the string 'searchString') then (ECHO found it!)
ELSE(ECHO not found)
到目前为止,我已经找到了一种使用 FIND 命令在文件中搜索字符串的方法,该命令返回行在找到字符串的文件中,但无法对其进行条件检查。
例如,这是行不通的。
IF FIND "searchString" file.txt ECHO found it!
这也不:
IF FIND "searchString" file.txt=='' ECHO not found
关于如何做到这一点有任何想法吗?
I am trying to create a batch (.bat) file for windows XP to do the following:
If (file.txt contains the string 'searchString') then (ECHO found it!)
ELSE(ECHO not found)
So far, I have found a way to search for strings inside a file using the FIND command which returns the line in the file where it finds the string, but am unable to do a conditional check on it.
For example, this doesn't work.
IF FIND "searchString" file.txt ECHO found it!
Nor does this:
IF FIND "searchString" file.txt=='' ECHO not found
Any Ideas on how this can be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
来自其他帖子:
当您想要不区分大小写的检查时,请使用 /i 开关:
或者类似的内容:如果找不到,则写入文件。
或者类似的东西:如果找到则写入文件。
或者类似的东西:
From other post:
Use the /i switch when you want case insensitive checking:
Or something like: if not found write to file.
Or something like: if found write to file.
Or something like:
我使用 DOS 命令行来执行此操作。
实际上是两条线。第一个将“当前目录”设为文件所在的文件夹 - 或文件所在的一组文件夹的根文件夹。第二行进行搜索。
您可以在此链接找到有关参数的详细信息。
希望有帮助!
I've used a DOS command line to do this.
Two lines, actually. The first one to make the "current directory" the folder where the file is - or the root folder of a group of folders where the file can be. The second line does the search.
You can find details about the parameters at this link.
Hope it helps!
另一个解决方案:
Another solution: