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

How to ignore a specific command-line error output from batch script?

我制作了一个批处理脚本,除其他外,使用以下命令将我们的 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”来避免 all errors/ouput,如下所示:

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

理想情况下,我只想 ignore/suppress 特别是 TF401190 错误。我觉得必须有一种方法可以做到这一点,即使这意味着在允许打印之前检查特定 token/string 的输出。我对命令行和批处理脚本还是很陌生。任何帮助将不胜感激!谢谢

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

在 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

(其他错误信息可以顺利通过)

这个问题的答案是 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."

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