Alex Rivera | Logout

Displaying Windows command prompt output and redirecting it to a file

Asked 2009-04-28T06:32:23.283
464

How can I run a command-line application in the Windows command prompt and have the output both displayed and redirected to a file at the same time?

If, for example, I were to run the command dir > test.txt, this would redirect output to a file called test.txt without displaying the results.

How could I write a command to display the output and redirect output to a file in the Windows command prompt, similar to the tee command on Unix?

Edit
Report

3 Answers

28

Unfortunately, there isn't any such thing.

Windows console applications only have a single output handle. (Well, there are two, standard output and standard error, but it doesn't matter here.) The > redirects the output, normally written to the console handle, to a file handle.

If you want to have some kind of multiplexing, you have to use an external application which you can divert the output to. This application then can write to a file and to the console again.

answered 2009-04-28T06:48:20.733
8

Use:

dir 1>a.txt 2>&1 | type a.txt

This will help to redirect both standard output and standard error.

answered 2011-11-02T15:23:07.303
3

Just like Unix:

dir | tee a.txt

It does work on Windows XP, bur it requires mksnt installed.

It displays on the prompt as well as appends to the file.

answered 2013-05-03T12:47:31.410

Your Answer