35

I want to capture the errors from a script into a file instead of to the screen.

In *nix, this is done with stderr redirection, usually

echo "Error" 2> errorfile.log

How do I do it in a CMD script under Windows?

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
mik
  • 1,458
  • 3
  • 14
  • 15

2 Answers2

44

For example:

PSKILL NOTEPAD >output.txt 2>&1

This will direct stdout and stderr to a file name output.txt.

See Underused features of Windows batch files for more details.

Community
  • 1
  • 1
aphoria
  • 18,540
  • 7
  • 58
  • 69
  • 7
    If you want them redirected to separate files, you can do `mycommand >stdout.txt 2>stderr.txt` – Kip Oct 24 '12 at 16:15
20

That should work in Win32, too.

If you have already redirected stdout, and want stderr redirected to the same file, you must use the 2>& special form, rather than just specifying the same file twice. Otherwise you'll get a "file busy" error.

unwind
  • 364,555
  • 61
  • 449
  • 578