views:

34

answers:

2

I have a batch file (.BAT) and would like to capture its output into a file automatically every time it is run. I know I can use redirection but then I wouldn't be able to watch it as it's running. Also, other people use this batch file and I want the log to be created without the user having to do anything special.

+2  A: 

Tee for windows? http://www.chipstips.com/?p=129

joni
Is there any way to use "tee" without making the user type it (meaning: they just run my batch file and the batch file somehow calls "tee" automatically)
JoelFan
@JoelFan: Yes, there is a simple way: Use another batch file (or the same batch file calling itself with different options).
0xA3
+1  A: 

If you're adverse to downloading or installing tee you can use a simple subroutine to emulate it:

rem log <message>
:log
echo.%*
1>>logfile.txt echo.%*

and then just output by using call :log Some crazy message. This will only work for your own messages, however, it doesn't do anything with programs you run from your batch. You could probably do that by introducing another subroutine:

rem runlog <program> <arguments>
:runlog
setlocal
set FN=%RANDOM%
1>%FN%.tmp 2>&1 %*
type %FN%.tmp
1>>logfile.txt type %FN%.tmp
del %FN%.tmp
endlocal
Joey