如何忽略批处理脚本的特定命令行错误输出?

时间:2015-07-09 18:40:05

标签: batch-file command-line error-suppression

我制作了一个批处理脚本,除其他外,使用以下命令将我们的DEV分支合并到我们的TEST分支:

tf merge $/Proj/Dev $/Proj/Test /recursive >nul

此命令始终触发以下输出:

TF401190: The local workspace [workspace];[name] has 110500 items in it, which exceeds the recommended limit of 100000 items. 
To improve performance, either reduce the number of items in the workspace, or convert the workspace to a server workspace.

我知道我可以通过在命令末尾添加“2>& 1”来避免所有错误/输出:

tf merge $/Proj/Dev $/Proj/Test /recursive >nul 2>&1

理想情况下,我只想忽略/抑制TF401190错误。我觉得必须有一种方法可以做到这一点,即使它意味着在允许打印之前检查特定标记/字符串的输出。我仍然是命令行和批处理脚本的新手。任何帮助将不胜感激!感谢。

注意:我对解决错误本身的解决方案不感兴趣。此问题仅涉及如何抑制任何特定的错误。

2 个答案:

答案 0 :(得分:1)

在bash shell中,您可以过滤掉这样的特定错误:

ls /nothere

ls: cannot access /nothere: No such file or directory

要取消该特定错误消息:

ls /nothere 2>&1 | grep -v 'No such file'

(错误消息被抑制)

检查是否有其他错误消息通过:

ls /root 2>&1 | grep -v 'No such file'
ls: cannot open directory /root: Permission denied

(其他错误信息正常)

答案 1 :(得分:0)

此问题的答案是对Is there a way to redirect ONLY stderr to stdout (not combine the two) so it can be piped to other programs?

的扩展

您需要以仅输出错误的方式重定向stderr和stdout,并将错误消息传递给FIND或FINDSTR命令,以过滤掉您不想要的消息。

tf merge $/Proj/Dev $/Proj/Test /recursive 2>&1 >nul | findstr /b ^
  /c:"TF401190: The local workspace " ^
  /c:"To improve performance, either reduce the number of items in the workspace, or convert the workspace to a server workspace."

我使用了行继续来使代码更容易阅读。

相关问题