0

I have a batch script that starts a server. This server then prints out its logs to the STDOUT and STDERR in case of errors.

I want to redirect both STDOUT and STDERR to a tee command to split the output. Using the tee command from UnxUtils this worked just fine.

However in order to make my application portable I cannot depend on UnxUtils so I need to write my own tee command.

I wrote a batch script that reads out the STDOUT like suggested in this answer (2. codeblock).

This works, but only for the redirected STDOUT and not for the STDERR.

Note: Output from STDERR is missing in both the file and the command line.

How can I make it read both streams?

How I redirect the output (%1 is the server, %2 is the file I want the logs to go to)?

%1 2>&1 | %~dp0tee.bat -a %2

This works fine (as it calls UnxUtils, not my batch script):

%1 2>&1 | tee -a %2

How I read the input in tee.bat:

for /F "tokens=*" %%a in ('findstr /n $') do (
    set "line=%%a"
    setlocal EnableDelayedExpansion
    set "line=!line:*:=!"

    echo(!line!

    if %append%==true (
        echo(!line! >> %output%
    ) else (
        echo(!line! > %output%
    )
    endlocal
)
Stephan
  • 53,940
  • 10
  • 58
  • 91
Lahzey
  • 373
  • 5
  • 17
  • Your answer is on this page http://stackoverflow.com/questions/41030190/command-to-run-a-bat-file/41049135#41049135 – Noodles May 02 '19 at 21:33

1 Answers1

1

PowerShell has a Tee-Object cmdlet. The $Input variable receives stdin.

echo asf 2>&1 | powershell -NoLogo -NoProfile -Command "$Input | Tee-Object -FilePath './tee.txt'"

See also: https://stackoverflow.com/a/38668360/447901

lit
  • 14,456
  • 10
  • 65
  • 119
  • I knew PowerShell had that command, however I did not know you could use it while still printing the output in the classic cmd. Thanks alot! – Lahzey May 06 '19 at 08:08