I have a batch script that executes a task and sends the output to a text file. Is there a way to have the output show on the console window as well?
For Example:
c:\Windows>dir > windows-dir.txt
Is there a way to have the output of dir
display in the console window as well as put it into the text file?
Best Answer
No, you can't with pure redirection.
But with some tricks (like tee.bat) you can.
I try to explain the redirection a bit.
You redirect one of the ten streams with > file or < file
It is unimportant, if the redirection is before or after the command, so these two lines are nearly the same.
The redirection in this example is only a shortcut for 1>, this means the stream 1 (STDOUT) will be redirected.
So you can redirect any stream with prepending the number like 2> err.txt and it is also allowed to redirect multiple streams in one line.
In this example the "standard output" will go into files.txt, all errors will be in err.txt and the stream3 will go into nothing.txt (DIR doesn't use the stream 3).
Stream0 is STDIN
Stream1 is STDOUT
Stream2 is STDERR
Stream3-9 are not used
But what happens if you try to redirect the same stream multiple times?
"There can be only one", and it is always the last one!
So it is equal to dir > two.txt
Ok, there is one extra possibility, redirecting a stream to another stream.
2>&1 redirects stream2 to stream1 and 1>files.txt redirects all to files.txt.
The order is important here!
are different. The first one redirects all (STDOUT and STDERR) to NUL,
but the second line redirects the STDOUT to NUL and STDERR to the "empty" STDOUT.
As one conclusion, it is obvious why the examples of Otávio Décio and andynormancx can't work.
Both try to redirect stream1 two times, but "There can be only one", and it's always the last one.
So you get
And in the first sample redirecting of stream1 to stream1 is not allowed (and not very useful).
Hope it helps.