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
)
1

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.