将 Windows cmd stdout 和 stderr 重定向到单个文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1420965/
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 06:43:11  来源:igfitidea点击:

Redirect Windows cmd stdout and stderr to a single file

windowscommand-linecmdpipe

提问by ripper234

I'm trying to redirect all output (stdout + stderr) of a DOScommand to a single file:

我正在尝试将DOS命令的所有输出(stdout + stderr)重定向到单个文件:

C:\>dir 1> a.txt 2> a.txt
The process cannot access the file because it is being used by another process.

Is it possible, or should I just redirect to two separate files?

是否可能,或者我应该重定向到两个单独的文件?

回答by Anders Lindahl

You want:

你要:

dir > a.txt 2>&1

The syntax 2>&1will redirect 2(stderr) to 1(stdout). You can also hide messages by redirecting to NUL, more explanation and examples on MSDN.

语法2>&12(stderr)重定向到1(stdout)。您还可以通过重定向到隐藏的信息NUL在MSDN上更多的解释和例子

回答by DelboyJay

Anders Lindahl's answer is correct, but it should be noted that if you are redirecting stdout to a file and want to redirect stderr as well then you MUST ensure that 2>&1is specified AFTERthe 1>redirect, otherwise it will not work.

Anders Lindahl 的回答是正确的,但应该注意的是,如果您将 stdout 重定向到一个文件并且还想重定向 stderr,那么您必须确保在重定向之后2>&1指定,否则它将无法工作。1>

REM *** WARNING: THIS WILL NOT REDIRECT STDERR TO STDOUT ****
dir 2>&1 > a.txt

回答by StormeHawke

Background info from MSKB

来自 MSKB 的背景信息

While the accepted answer to this question is correct, it really doesn't do much to explain whyit works, and since the syntax is not immediately clear I did a quick google to find out what was actually going on. In the hopes that this information is helpful to others, I'm posting it here.

虽然这个问题的公认答案是正确的,但它确实没有太多解释它为什么起作用,而且由于语法不是很清楚,我做了一个快速的谷歌来找出实际发生的事情。希望这些信息对其他人有帮助,我将其发布在这里。

Taken from MS Support KB 110930.

取自MS 支持知识库 110930



From MSKB110930

来自 MSKB110930

Redirecting Error Messages from Command Prompt: STDERR/STDOUT

Summary

When redirecting output from an application using the '>' symbol, error messages still print to the screen. This is because error messages are often sent to the Standard Error stream instead of the Standard Out stream.

Output from a console (Command Prompt) application or command is often sent to two separate streams. The regular output is sent to Standard Out (STDOUT) and the error messages are sent to Standard Error (STDERR). When you redirect console output using the ">" symbol, you are only redirecting STDOUT. In order to redirect STDERR you have to specify '2>' for the redirection symbol. This selects the second output stream which is STDERR.

Example

The command dir file.xxx(where file.xxxdoes not exist) will display the following output:

Volume in drive F is Candy Cane Volume Serial Number is 34EC-0876

File Not Found

If you redirect the output to the NULdevice using dir file.xxx > nul, you will still see the error message part of the output, like this:

File Not Found

To redirect (only) the error message to NUL, use the following command:

dir file.xxx 2> nul

Or, you can redirect the output to one place, and the errors to another.

dir file.xxx > output.msg 2> output.err

You can print the errors and standard output to a single file by using the "&1" command to redirect the output for STDERR to STDOUT and then sending the output from STDOUT to a file:

dir file.xxx 1> output.msg 2>&1

从命令提示符重定向错误消息:STDERR/STDOUT

概括

使用“>”符号重定向应用程序的输出时,错误消息仍会打印到屏幕上。这是因为错误消息通常被发送到标准错误流而不是标准输出流。

控制台(命令提示符)应用程序或命令的输出通常发送到两个单独的流。常规输出发送到标准输出 (STDOUT),错误消息发送到标准错误 (STDERR)。当您使用“>”符号重定向控制台输出时,您只是在重定向 STDOUT。为了重定向 STDERR,您必须为重定向符号指定“2>”。这将选择第二个输出流,即 STDERR。

例子

该命令dir file.xxx(其中file.xxx不存在)将显示以下输出:

Volume in drive F is Candy Cane Volume Serial Number is 34EC-0876

File Not Found

如果您使用 将输出重定向到NUL设备dir file.xxx > nul,您仍然会看到输出的错误消息部分,如下所示:

File Not Found

要将错误消息(仅)重定向到NUL,请使用以下命令:

dir file.xxx 2> nul

或者,您可以将输出重定向到一个地方,将错误重定向到另一个地方。

dir file.xxx > output.msg 2> output.err

通过使用“&1”命令将 STDERR 的输出重定向到 STDOUT,然后将输出从 STDOUT 发送到文件,您可以将错误和标准输出打印到单个文件中:

dir file.xxx 1> output.msg 2>&1

回答by Henk Wiersema

To add the stdout and stderr to the general logfile of a script:

要将 stdout 和 stderr 添加到脚本的常规日志文件中:

dir >> a.txt 2>&1

回答by Max Vitesse

Correct, file handle 1 for the process is STDOUT, redirected by the 1>or by >(1 can be omitted, by convention, the command interpreter [cmd.exe] knows to handle that). File handle 2 is STDERR, redirected by 2>.

正确,进程的文件句柄 1 是 STDOUT,由1>or重定向>(1 可以省略,按照惯例,命令解释器 [cmd.exe] 知道处理它)。文件句柄 2 是 STDERR,由2>.

Note that if you're using these to make log files, then unless you're sending the outut to _uniquely_named_ (eg date-and-time-stamped) log files, then if you run the same process twice, the redirected will overwrite (replace) the previous log file.

请注意,如果您使用这些来制作日志文件,那么除非您将输出发送到 _uniquely_named_(例如日期和时间戳)日志文件,否则如果您运行相同的进程两次,重定向的将覆盖(替换)之前的日志文件。

The >>(for either STDOUT or STDERR) will APPEND not REPLACE the file. So you get a cumulative logfile, showwing the results from all runs of the process - typically more useful.

>>(对于stdout或stderr)将追加不替换该文件。所以你会得到一个累积的日志文件,显示所有进程运行的结果 - 通常更有用。

Happy trails...

快乐的轨迹...

回答by LigH

There is, however, no guarantee that the output of SDTOUTand STDERRare interweaved line-by-line in timely order, using the POSIXredirect merge syntax.

然而,有没有保证的输出SDTOUTSTDERR被交织行由行及时订购,使用POSIX重定向合并语法。

If an application uses buffered output, it may happen that the text of one stream is inserted in the other at a buffer boundary, which may appear in the middle of a text line.

如果应用程序使用缓冲输出,则可能会发生一个流的文本在缓冲区边界处插入另一个流中,该边界可能出现在文本行的中间。

A dedicated console output logger (I.e. the "StdOut/StdErr Logger"by 'LoRd MuldeR') may be more reliable for such a task.

对于这样的任务,专用的控制台输出记录器(即"StdOut/StdErr Logger"by 'LoRd MuldeR')可能更可靠。

See: MuldeR's OpenSource Projects

请参阅:MuldeR 的开源项目

回答by PanamaPHat

In a batch file (Windows 7 and above) I found this method most reliable

在批处理文件(Windows 7 及更高版本)中,我发现这种方法最可靠

Call :logging >"C:\Temp\NAME_Your_Log_File.txt" 2>&1
:logging
TITLE "Logging Commands"
ECHO "Read this output in your log file"
ECHO ..
Prompt $_
COLOR 0F

Obviously, use whatever commands you want and the output will be directed to the text file. Using this method isreliable HOWEVER there is NO output on the screen.

显然,使用您想要的任何命令,输出将被定向到文本文件。使用这种方法可靠的,但是屏幕上没有输出。