如何将Windows命令提示符中的标准输出和错误输出重定向至一个文件?

835

我想将所有Windows命令行的输出(包括stdout和stderr)重定向到一个单独的文件中:

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

这是否可行,还是我应该重定向到两个单独的文件?


24
TechNet: 使用命令重定向运算符(比这里的任何答案都更好地回答了这个问题)。 - Martin Prikryl
2
2>&1 无法重新打开同一文件。 - Luke
7个回答

1323

41
谢谢,我不知道这个Unix Shell语法也适用于DOS! - chaindriver
25
这个命令非常适合隐藏所有输出.. net stop w3svc >NUL 2>&1.. 谢谢! - kodybrown
4
@wasatchwizard,我想我遇到了麻烦,但>NUL 2>NUL很好用。 - FrinkTheBrave
18
如果存在一个句柄(即2),则句柄和重定向运算符(即>)之间不能有空格。因此,2> 2.txt是可以工作的(或者2>&1),而2 > 2.txt不行;2 > &1也不行。 - Nate Anderson
3
很遗憾微软没有本地化的Tee。 - JJS
显示剩余6条评论

239

Anders Lindahl的答案是正确的,但需要注意的是,如果您正在将stdout重定向到文件并想要同时重定向stderr,则必须确保在1>重定向之后指定2>&1,否则它不起作用。

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

16
AFTER是什么让我花了数小时才弄清楚哪里出了问题,DelboyJay!谢谢你! - Nam G VU
5
有没有解释为什么在1>之前放置2>&1不能实现预期效果?我强烈怀疑这与“cmd”解析命令的方式有关,具体取决于指定重定向的顺序。但是语义规则是否有记录,因为我认为这是值得学习的东西,因为它可能会浪费数小时时间。 - igbgotiz
14
@igbgotiz 2>&1 的意思是“将流2重定向到流1”。因此,您需要先设置流1。 - FrinkTheBrave
5
@FrinkTheBrave,但是如果没有明确指定,流1就是标准输出(例如控制台)。但这仍然不能说明它的含义。 - MarioDS
1
@MDeSchaepmeester,如果你执行dir 2>&1 > a.txt,那么你首先将流2(stderr)重定向到流1(stdout)。然后,在它们都已经合并在一起之后,你将stdout(没有指定符号的>)重定向到文件中。如果你想让stderr去别的地方,你不能先将它与stdout合并。 - cp.engr
显示剩余9条评论

97

来自微软文档的背景信息

虽然这个问题的被接受答案是正确的,但它并没有很好地解释为什么它有效,而且由于语法不是立即清晰的,我进行了快速的www搜索以找出实际发生了什么。希望这些信息对其他人有所帮助,我在这里发布它。

摘自微软文档页面:
从命令提示符重定向错误消息: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 that is STDERR.

Example

The command dir file.xxx (where file.xxx does 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 NUL device using dir file.xxx > nul, you will still see the error message:

File Not Found

To redirect 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

43

要将脚本的标准输出(stdout)和标准错误(stderr)添加到通用日志文件中:

dir >> a.txt 2>&1

21
>> 追加到文件,而 > 覆盖文件。 - delliottg

15

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

请注意,如果您使用它们来创建日志文件,除非将输出发送到具有唯一名称(例如日期和时间戳)的日志文件,否则如果您运行同一进程两次,则重定向将覆盖(替换)先前的日志文件。

>>(用于STDOUT或STDERR)将附加而不是替换文件。因此,您会得到一个累积的日志文件,显示进程的所有运行结果-通常更有用。

祝好运...


5
然而,使用POSIX重定向合并语法,不能保证SDTOUTSTDERR的输出按时交织在一起。
如果应用程序使用缓冲输出,可能会发生一个流的文本插入到另一个流中,在文本行的中间出现缓冲边界。
一个专用的控制台输出记录器(例如"StdOut/StdErr Logger" by 'LoRd MuldeR')可能更可靠地完成这样的任务。
参见:MuldeR's OpenSource Projects

4
在批处理文件(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

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


2
你可以使用 >con 强制输出到屏幕,例如 echo This goes to screen。这对于用户输入也很有用,例如 set /p "var="Input: "。请注意,这些行将仅出现在屏幕上,而不会被重定向到文件中。 - Stephan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接