Reputation: 410
I'm currently using wintee to log the result messages from testing scripts. Here's what the command looks like:
test_name.bat [parameters] 2>&1 | wtee log.txt
However, I only would like to archive STDERR to a file, while still displaying both STDOUT and STDERR to the console. The problem emerges from wintee's limitation: it seems to only fork STDIN, STDOUT, and input files.
Here's what I'm trying to do:
This way, the console will still display both streams' messages, while wintee will only fork the messages from (what originally was) STDERR.
However, I'm not sure whether if it's possible, as my understanding of the stream redirection is shallow. I'm trying to see if I can redirect STDOUT to STDERR using another stream in between:
echo Hello World! 3>&2 >3
However, it isn't printing anything.
Is what I'm trying to accomplish possible in native batch (optionally with helps of other tools)? If so, what am I doing wrong?
Upvotes: 2
Views: 1213
Reputation:
Set Arg = WScript.Arguments
set WshShell = createObject("Wscript.Shell")
Set Inp = WScript.Stdin
Set Outp = Wscript.Stdout
On Error Resume Next
Set Fso = CreateObject("Scripting.FileSystemObject")
Set File = Fso.CreateTextFile(Arg(0), True)
If err.number <> 0 then
Outp.WriteLine "Error: " & err.number & " " & err.description & " from " & err.source
err.clear
wscript.exit
End If
Do Until Inp.AtEndOfStream
Line=Inp.readline
outp.writeline Line
File.WriteLine Line
Loop
This Tees StdOut. Changing the 4th line to Set Outp = Wscript.Stderr
will make it Tee StdErr.
To use dir | cscript //nologo Tee.vbs
.
Upvotes: 2
Reputation: 34919
I do not know the wintee
command, but I think I can help you with the redirection issue:
Instead of test_name.bat [parameters] 2>&1 | wtee log.txt
, you should write the following:
(test_name.bat [parameters] 2>&1 1> con) | wtee log.txt
This writes the text at STDOUT to the console, redirects the data at STDERR to STDOUT, which is in turn passed to the wtee
command.
Note that the console displays all original STDOUT before any STDERR data, because the former is displayed immediately, while the latter is passed through wtee
. With pure redirection hacks it is not possible to preserve the original order which the data was returned with. If you insist on that, you need to use a tool other than Edit: In particular, the pipe is the bottleneck, because there is only one channel, namely STDIN, where it passes data to. So if you insist on STDOUT and SRDERR data to be displayed in the original order while saving the data of one stream to a file, you have no other choice but to modify the script wintee
which has got the required capabilities.test_name.bat
and to avoid piping.
I am trying to explain that using the command dir ":"
on the left side of the pipe, which produces output at both STDOUT and STDERR (because of the invalid path ":"
):
D:\Data> dir ":"
Volume in drive D has no label.
Volume Seriel Number is 0000-0000
Directory of D:\Data
File Not Found
The File Not Found
message appears at STDERR, whereas the rest appears at STDOUT (you can prove that by redirecting like 2> nul
or 1> nul
to dismiss either of the streams).
On the right side of the pipe, I am using the command find /V ""
, which simply passes all data it receives at STDIN through and displays it on the console:
D:\Data> dir ":" | find /V ""
File Not Found
Volume in drive D has no label.
Volume Seriel Number is 0000-0000
Directory of D:\Data
The changed order of the console output illustrates what happens: STDERR is displayed immediately, whereas STDOUT is first passed through the pipe before being displayed.
Now let us apply the redirection 2>&1
from your command line:
D:\Data> (dir ":" 2>&1) | find /V ""
Volume in drive D has no label.
Volume Seriel Number is 0000-0000
Directory of D:\Data
File Not Found
This redirects STDERR to STDOUT, so the original STDOUT data together with the redirected ones are piped through. Replacing find /V ""
by (> nul find /V "")
proves that the right side of the pipe really receives all of the data.
Now let us add the 1> con
portion, which constitutes an explicit redirection of STDOUT to the console:
D:\Data> (dir ":" 2>&1 1> con) | find /V ""
Volume in drive D has no label.
Volume Seriel Number is 0000-0000
Directory of D:\Data
File Not Found
The output contains all of the original data. Again replacing find /V ""
by (> nul find /V "")
proves that this time, the right side of the pipe truly only recieves the File Not Found
message, which was originally present at STDERR, but the STDOUT data are not piped through.
Just a side-note:
If you want to do something like wintee
with pure batch-file, things become very complicated -- see this example...
Upvotes: 1