This is not another answer, but more an overview and clarification to the already existed answers like
Displaying Windows command prompt output and redirecting it to a file
and others
I've found for myself that there is a set of issues what makes a set of tee implementations are not reliable in the Windows (Windows 7
in mine case).
I need to use specifically a tee implementation because have already uses a batch script with self redirection:
@echo off
setlocal
... some conditions here ..
rem the redirection
"%COMSPEC%" /C call %0 %* 2>&1 | "<path_to_tee_utililty>" ".log\<log_file_name_with_date_and_time>.%~nx0.log"
exit /b
:IMPL
... here the rest of script ...
The script and calls to some utilities inside the script can break the output if used together with a tee utility.
- The gnuwin32 implementation:
http://gnuwin32.sourceforge.net/packages/coreutils.htm
Pros:
- Correctly handles standard output together with a console progress bar, where the
\r
character is heavily used.
Cons:
- Makes console progress bars to draw only in a log file, but it has not duplicated or visible in the console window.
- Throws multiple error messages
Cwrite error: No such file or directory
because seems the cmd interpreter closes the pipe/stdout too early and can not self close after that (spamming until termination).
- Does not duplicate/print the output from the
pause
command (Press any key to continue...
) in the console window.
- The wintee implementation:
https://code.google.com/archive/p/wintee/
https://github.com/rbuhl/wintee
Pros:
- Shows a console progress bar both in the console window and in a log file (multiple prints).
- Does duplicate/print the output from the
pause
command (Press any key to continue...
) in the console window.
Cons:
- The UnxUtils implementation:
http://unxutils.sourceforge.net/
https://sourceforge.net/projects/unxutils/files/unxutils/current/
Pros
- Shows a console progress bar both in the console window and in a log file (multiple prints).
- Correctly handles the
\r
character.
- Does duplicate/print the output from the
pause
command (Press any key to continue...
) in the console window.
Cons
Not yet found
- The ss64.net implementation:
http://ss64.net/westlake/nt
http://ss64.net/westlake/nt/tee.zip
Pros:
- Shows a console progress bar both in the console window and in a log file (multiple prints).
Cons:
- Incorrectly handles the
\r
character, output is mixed and messed
- For some reason does duplicate/print the output from the
pause
command (Press any key to continue...
) in the console window AFTER a key press.
- The ritchielawrence mtee implementation:
https://ritchielawrence.github.io/mtee
https://github.com/ritchielawrence/mtee
Pros
- Shows a console progress bar both in the console window and in a log file (multiple prints).
- Correctly handles the
\r
character.
- Does duplicate/print the output from the
pause
command (Press any key to continue...
) in the console window.
- The error code retain feature w/o a need to use workaround with the
doskey
(/E
flag, Windows command interpreter: how to obtain exit code of first piped command )
Cons
So, if you are choosing the tee utility implementation between the above, then a better choice is the UnxUtils
or mtee
.
If you are searching for a better implementation with more features and less issues, then you can use callf
utility:
https://github.com/andry81/contools/tree/HEAD/Utilities/src/callf/help.tpl
You can run instead of:
call test.bat | mtee /E 1.log
This:
callf.exe /ret-child-exit /tee-stdout 1.log /tee-stdout-dup 1 "" "cmd.exe /c call test.bat"
It is better because it can pipe stdout
separately from stderr
and you can even pipe between processes with Administrator privileges isolation using named pipes.