DOS:查找一个字符串,如果找到则运行另一个脚本
我想使用 DOS 在文件中查找字符串:
例如
查找“字符串”status.txt
当找到它时,我想运行一个批处理文件。
最好的方法是什么?
I want to find a string in a file using DOS:
For example
find "string" status.txt
And when it is found, I want to run a batch file.
What is the best way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我已经有一段时间没有对批处理文件做过任何事情了,但我认为以下方法有效:
这确实是一个概念证明;根据您的需要进行清理。关键是,如果
string
不在file
find 将返回1
的errorlevel
>。在这种情况下,我们分支到notfound
,否则我们处理found
情况。It's been awhile since I've done anything with batch files but I think that the following works:
This is really a proof of concept; clean up as it suits your needs. The key is that
find
returns anerrorlevel
of1
ifstring
is not infile
. We branch tonotfound
in this case otherwise we handle thefound
case.我们有两个命令,第一个是“condition_command”,第二个是“result_command”。
如果我们需要在“condition_command”成功时运行“result_command”(错误级别=0):
如果我们需要在“condition_command”失败时运行“result_command”:
因此,如果文件中有“string”,则运行“some_command” “status.txt”:
如果文件“status.txt”中没有“string”:
We have two commands, first is "condition_command", second is "result_command".
If we need run "result_command" when "condition_command" is successful (errorlevel=0):
If we need run "result_command" when "condition_command" is fail:
Therefore for run "some_command" in case when we have "string" in the file "status.txt":
in case when we have not "string" in the file "status.txt":
由于答案被标记为正确,因此它是一个 Windows Dos 提示脚本,这也将起作用:
As the answer is marked correct then it's a Windows Dos prompt script and this will work too:
这更简单:
That's more straightforward: