Redirecting Errors to a File in Windows Batch Scripts
In my post on Redirecting Output to a File in Windows Batch Scripts, I discussed the basics of output redirection into a text file and how this can be used for logging, including error logging by using the >& operator to combine the stderr and stdout streams. But Mark asked, “How do you just pipe stderr without joining it to stdout?” Good question!
So normal output redirection is handled with just the > or >> operators (depending on whether you want to overwrite or append the target file), like so:
dir >> myfile.txt
The operator outputs the stdout stream by default. But it can be modified to output the stderr stream by just specifying that stream with the operator. Remember, stderr is stream 2. So, you would do like so:
dir 2>> myfile.txt
This would write only the error output to the file.

March 9th, 2011 at 11:55 pm
No Josh, I know _that_. Every child in kindergarten knows that
I was asking how to _pipe_, not how to redirect.
Normally, foo | bar , pipes the standard output of foo, to bar, and the standard error still goes to the console, ignored by the pipe.
I want to pipe the standard error instead, and just output the standard output.
March 15th, 2011 at 9:16 am
Mark: Oh, well, I’m sorry to hear I’m boring the kindergarteners!
Why exactly are you doing this? Maybe if I have a little context, I can understand better.
March 24th, 2011 at 12:29 pm
Thanks for the blog! I found this useful. I had never seen that syntax to redirect standard error. I didn’t realize Windows wouldn’t redirect both standard output and error when using the “>” or “>>”. My cranky bit is usually flipped to on when dealing with Windows
March 25th, 2011 at 8:21 am
Lol, amen. Thanks for the comment.
May 31st, 2011 at 3:30 am
Maybe something like this can help:
mycommand >0 2>& | myothercommand
?
June 20th, 2011 at 1:38 pm
Can you consequently explain about this commande doing?
mycommand >0 2>& | myothercommand
The redirection is working well with the pipe include to the command.
Jacob.
October 17th, 2011 at 3:28 am
Nice post, thanks
November 7th, 2011 at 12:26 am
Great post! Simple script you shared here for sure I can make it a bit interesting. Thanks for sharing.
November 7th, 2011 at 12:28 am
Finally thanks for this opportunity that you shared to us this is so absolutely useful blog..Thanks!!
November 7th, 2011 at 12:29 am
I didn’t realize Windows wouldn’t redirect both standard output and error when using the “>” or “>>”. Thanks for the big help.
November 7th, 2011 at 12:39 am
When using redirection to create temporary batch files, keep in mind that the output that you redirect may vary with different language versions.