findstr 退出代码/错误级别

Posted

技术标签:

【中文标题】findstr 退出代码/错误级别【英文标题】:findstr exit codes/errorlevel 【发布时间】:2015-08-04 13:41:19 【问题描述】:

我有一个使用findstr 的程序,当找到字符串时errorlevel 返回0,当找不到字符串时errorlevel 返回1。好吧,没关系,我能应付。

问题出在我找不到任何官方文档说明每个errorlevelfindstr 的含义。我需要知道findstr 的其他任何东西是否可以返回1errorlevel,或者它是否只在找不到字符串时返回1

“官方”文档的链接是首选,如果有的话,但任何输入将不胜感激。

提前致谢!

【问题讨论】:

【参考方案1】:

http://ss64.com/nt/findstr.html 说:

FINDSTR will set %ERRORLEVEL% as follows:

0 (False) a match is found in at least one line of at least one file.
1 (True) if a match is not found in any line of any file, (or if the file is not found at all).
2 Wrong syntax 
An invalid switch will only print an error message in error stream.

【讨论】:

【参考方案2】:

在 FIND 命令的 Dos 6.22 帮助中有记录。

│FIND exit codes
│
│The following list shows each exit code and a brief description of its
│meaning:
│
│0
│    The search was completed successfully and at least one match was found.
│
│1
│    The search was completed successfully, but no matches were found.
│
│2
│    The search was not completed successfully. In this case, an error
│    occurred during the search, and FIND cannot report whether any matches
│    were found.
│
│You can use the ERRORLEVEL parameter on the <If> command line in a batch
│program to process exit codes returned by FIND.

【讨论】:

这是否也适用于`findstr'? 是的。我搜索了 12 年的最新参考资料。我有 Dos 6.22 和 OS/2 Warp 4(它说 它返回 0 表示正常完成)。

以上是关于findstr 退出代码/错误级别的主要内容,如果未能解决你的问题,请参考以下文章

findstr的命令行分析机制

搜索过滤grep(win下为findstr)

PowerShell FINDSTR 等价物?

findstr

FINDSTR正则表达式的基本用法

Batch - FINDSTR