我想将所有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.
这是否可行,还是我应该重定向到两个单独的文件?
我想将所有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.
这是否可行,还是我应该重定向到两个单独的文件?
您希望:
dir > a.txt 2>&1
语法2>&1
将把2
(stderr)重定向到1
(stdout)。您还可以通过重定向到NUL
来隐藏消息。有关更多说明和示例,请参阅Microsoft文档页面"从命令提示符重定向错误消息:STDERR/STDOUT"。
net stop w3svc >NUL 2>&1
.. 谢谢! - kodybrown2> 2.txt
是可以工作的(或者2>&1
),而2 > 2.txt
不行;2 > &1
也不行。 - Nate AndersonAnders Lindahl的答案是正确的,但需要注意的是,如果您正在将stdout重定向到文件并想要同时重定向stderr,则必须确保在1>
重定向之后指定2>&1
,否则它不起作用。
REM *** WARNING: THIS WILL NOT REDIRECT STDERR TO STDOUT ****
dir 2>&1 > a.txt
dir 2>&1 > a.txt
,那么你首先将流2(stderr)重定向到流1(stdout)。然后,在它们都已经合并在一起之后,你将stdout(没有指定符号的>
)重定向到文件中。如果你想让stderr去别的地方,你不能先将它与stdout合并。 - cp.engr虽然这个问题的被接受答案是正确的,但它并没有很好地解释为什么它有效,而且由于语法不是立即清晰的,我进行了快速的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 specify2>
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 FoundIf 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
要将脚本的标准输出(stdout)和标准错误(stderr)添加到通用日志文件中:
dir >> a.txt 2>&1
>>
追加到文件,而 >
覆盖文件。 - delliottg正确的,进程的文件句柄1是标准输出(STDOUT),由1>
或>
重定向(1可以省略,按照惯例,命令解释器[cmd.exe]会处理它)。
文件句柄2是标准错误(STDERR),由2>
重定向。
请注意,如果您使用它们来创建日志文件,除非将输出发送到具有唯一名称(例如日期和时间戳)的日志文件,否则如果您运行同一进程两次,则重定向将覆盖(替换)先前的日志文件。
>>
(用于STDOUT或STDERR)将附加而不是替换文件。因此,您会得到一个累积的日志文件,显示进程的所有运行结果-通常更有用。
祝好运...
POSIX
重定向合并语法,不能保证SDTOUT
和STDERR
的输出按时交织在一起。"StdOut/StdErr Logger"
by 'LoRd MuldeR'
)可能更可靠地完成这样的任务。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
显然,您可以使用任何命令,并将输出定向到文本文件中。使用这种方法是可靠的,但是屏幕上没有输出。
>con
强制输出到屏幕,例如 echo This goes to screen
。这对于用户输入也很有用,例如 set /p "var="Input: "
。请注意,这些行将仅出现在屏幕上,而不会被重定向到文件中。 - Stephan