create a log from a batch file
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 开发者_如何学Gocreated without the user having to do anything special.
Tee for windows? http://www.chipstips.com/?p=129
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
精彩评论