You are not logged in.

#1 08 Mar 2007 18:51

jduff
Member
Registered: 04 Mar 2007
Posts: 9

Redirection Tee to Two Streams

I would like to redirect a command line batch script's output to two places, e.g., stdout AND a log file. I want the user to see the output in the command window, and in a log.txt file for reference at a later time.

For echo command output, I can use the ugly technique of repeating the echo twice (very bad style). See below:

echo Begin My Script Processing
echo Begin My Script Processing 1>> myLogFile.txt

In addition to being bad style, this techique doesn't work for invoking an exe since I wouldn't want to invoke the exe twice.

Is there a way to split the stream in a batch command script and send it to two places at once?

jduff

Offline

#2 09 Mar 2007 19:18

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: Redirection Tee to Two Streams

I'm not aware of a native function in Windows to "tee" output in this fashion.  You're probably going to have to suck it up and install cygwin, or find a copy of "tee" ported to Win32. Actually, this might help, though I've not tried it, just did a quick google. smile

Depending on how you want it to work, I have a few methods I use in scripts where I need duplicate output to the screen and file, and I don't want to resort to external methods.

* Untested * I generally do more stuff in addition to what you see here, this is just to give you the general idea -- but the scripts with that code are at work. smile

@echo off

set log=whatever.log
set exe=whatever.exe
set stdout=whatever.stdout
set stderr=whatever.stderr

call :log Log this text
call :runexe "%exe%" /switch

goto :EOF

:log

for %%x in ("%log%" "con") do echo:%* >>"%%~x"

goto :EOF

:runexe

start /b /w "%~nx1" %* 1>"%stdout%" 2>"%stderr%"

for %%s in ("%stdout%" "%stderr%") do (
    for %%o in ("%log%" "con") do (
        type "%%~s" >> "%%~o"
    )
)

goto :EOF

N.B. One caveat is that the exe output will not appear in real time -- only once it has terminated

Last edited by bluesxman (15 Mar 2007 17:29)


cmd | *sh | ruby | chef

Offline

#3 15 Mar 2007 16:36

jduff
Member
Registered: 04 Mar 2007
Posts: 9

Re: Redirection Tee to Two Streams

Yes, I see. ...A bit round-about, but it solves the problem.

Thanks,
jduff

Offline

Board footer

Powered by