如何在 Windows/DOS 上捕获 stderr?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/482678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:59:03  来源:igfitidea点击:

How to capture stderr on Windows/DOS?

windowsscriptingbatch-filecmdstderr

提问by mik

I want to capture the errors from a script into a file instead of to the screen.

我想将脚本中的错误捕获到文件而不是屏幕中。

In *nix, this is done with stderr redirection, usually

在 *nix 中,这是通过 stderr 重定向完成的,通常

echo "Error" 2> errorfile.log

How do I do it in a CMD script under Windows?

如何在 Windows 下的 CMD 脚本中执行此操作?

回答by aphoria

For example:

例如:

PSKILL NOTEPAD >output.txt 2>&1

This will direct stdout and stderr to a file name output.txt.

这会将 stdout 和 stderr 定向到文件名 output.txt。

See Underused features of Windows batch filesfor more details.

有关详细信息,请参阅Windows 批处理文件的未充分利用的功能

回答by unwind

That should work in Win32, too.

这也应该适用于 Win32。

If you have already redirected stdout, and want stderr redirected to the same file, you must use the 2>& special form, rather than just specifying the same file twice. Otherwise you'll get a "file busy" error.

如果您已经重定向 stdout,并且希望将 stderr 重定向到同一个文件,则必须使用 2>& 特殊形式,而不仅仅是两次指定同一个文件。否则,您将收到“文件繁忙”错误。