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.

23 Responses to “Redirecting Errors to a File in Windows Batch Scripts”

  1. Mark Says:

    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.

  2. Josh Says:

    Mark: Oh, well, I’m sorry to hear I’m boring the kindergarteners! :-D Why exactly are you doing this? Maybe if I have a little context, I can understand better.

  3. Xanthros Says:

    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 :)

  4. Josh Says:

    Lol, amen. Thanks for the comment.

  5. Evengard Says:

    Maybe something like this can help:

    mycommand >0 2>& | myothercommand
    ?

  6. lampe led Says:

    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.

  7. la roulette Says:

    Nice post, thanks :D

  8. Lucilly Brown Says:

    Great post! Simple script you shared here for sure I can make it a bit interesting. Thanks for sharing.

  9. Debbie Curtis Says:

    Finally thanks for this opportunity that you shared to us this is so absolutely useful blog..Thanks!!

  10. Donna E. Steele Says:

    I didn’t realize Windows wouldn’t redirect both standard output and error when using the “>” or “>>”. Thanks for the big help.

  11. Kris Says:

    When using redirection to create temporary batch files, keep in mind that the output that you redirect may vary with different language versions.

  12. jeu roulette Says:

    Thanks. Very interseting!

  13. meilleur taux Says:

    I ike your blog. Very interesting. Thanks!

  14. bonus casino Says:

    Great blog! Thanks!

  15. argent facile Says:

    Very interesting thanks!

  16. blackjack Says:

    Interesting post! Thanks!

  17. casino francais Says:

    Usefull post! Thanks for sharing!

  18. jeu de casino gratuit Says:

    Great billet thanks!

  19. devenir riche Says:

    Thanks for sharing! Very interesting!

  20. jeu de loterie Says:

    Great post!

  21. jeu de voiture en ligne Says:

    Very interesting! Thanks!

  22. casino gratuit Says:

    Usefull!

  23. Lampe leds Says:

    C’est cool ça

  Theme Brought to you by Directory Journal and Elegant Directory.