Why simple redirection does not work in Windows cmd?

I want output of any command not just to show on console, but also to file. I have used simple command,

dir > out.txt 1>&2

Now what I am assuming here in command is, output of dir will go to out.txt, and 1 which is stdout will be redirected to 2 which is stderr.

My problem is that the output is being shown in the console but not being written to out.txt

Now console will show both stdout and stderr, so I am using stderr to show my output on console.

Please consider, their may be any other way to do the same thing. But I wanted to know what is problem with my logic.


Each stream has only one target. In your code

dir > out.txt 1>&2

you are requesting that the target for data sent by dir to stdout will be a file, and just after it, you change again the target for stdout stream, assigning it to a duplicate of the target in stderr stream.

There is no way to do it with only redirection operators. You will need some kind of tee utility. There is a good collection of answers here at SO giving you solutions coded in several lenguages.

链接地址: http://www.djcxy.com/p/92308.html

上一篇: Windows将stdout和stderr重定向到一个文件

下一篇: 为什么简单的重定向在Windows cmd中不起作用?