Display output of batch file on the screen and log file?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Display output of batch file on the screen and log file?

#1 Post by MKANET » 18 Apr 2012 20:23

I have way too many lines of code to try to echo the output of each command twice (once to the screen and once redirected to a log file).

I was hoping there was a way to capture the output of a batch file while it's running; without compromising what you see on the screen. If there's no way to do this natively in dos, I'm open to using something externally as long as it's very small and discrete (such as single executable or vbscript).

The tricky thing is my batch file calls a couple of other batch files while running.

Thanks in advance!

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: Display output of batch file on the screen and log file?

#2 Post by MKANET » 18 Apr 2012 21:07

I found the utility called wtee.exe (which works similar to the Unix command). However, unfortunately, it can't handle CLS properly. It coughs up a weird symbol and prevents the use of CLS on the local console.

Hopefully someone here knows how to do this. Surely I can't be the first person who needs to do this.

Ocalabob
Posts: 79
Joined: 24 Dec 2010 12:16
Location: Micanopy Florida

Re: Display output of batch file on the screen and log file?

#3 Post by Ocalabob » 18 Apr 2012 22:13

Greetings MKANET!
Would a simple 'tee' batch script help you out? Or am I missing the question.

An example for your consideration:

Code: Select all

@echo off
Setlocal
::Tee test example
set stamp=%date% at %time%
call :screen
call :log

pause

:screen
echo Hello World on %stamp%
echo How is life?
echo Put more script here.
goto :eof

:log
echo Hello World on %stamp%>file_log.txt
echo How is life?>>file_log.txt
echo Put more script here.>>file_log.txt
goto :eof


Best wishes!

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: Display output of batch file on the screen and log file?

#4 Post by MKANET » 18 Apr 2012 22:25

Thanks for replying. I'm not sure I follow. Where to put my 500 line batch file? :)

Just to be clear, the batch file is already written. I'm obviously not going to redo the entire batch file just to get a copy of the output.


foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Display output of batch file on the screen and log file?

#6 Post by foxidrive » 19 Apr 2012 01:06

I did this in the Win9x days using a tee filter but there wasn't anything useful and simple, for NT at that time.

If you don't have any interactive commands and can live without seeing the output until it is finished then this will work:

Code: Select all

@echo off
batchfile.bat >logfile.txt
type logfile.txt |more

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: Display output of batch file on the screen and log file?

#7 Post by MKANET » 19 Apr 2012 07:56

Yes, I can run my batch file without any interactive prompts. However, the output needs to be shown as it's coming in. It can't just wait until the entire batch file is finished and dump the entire contents at once.

I've tried wtee,exe and mtee.exe. It looks like they both cough up CLS commands. I wonder why nobody ever thought of making those utilities CLS friendly.

Maybe there's another creative way to do this without


MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: Display output of batch file on the screen and log file?

#9 Post by MKANET » 19 Apr 2012 17:56

It sees like all the tee programs I've tried behave the same way when they see a CLS command. I've tried 5 different versions. :cry:

I guess I'm going to have to modify every single line in my batch file that echo's text to the console screen.

Could someone please tell me the easiest/most convenient possible way to make a small subroutine I can call to show a line being echoed (such as the below example) and send a copy of it to logfile.txt?

Here's an example of what I need echoed to the screen and a copy of it appended to logfile.txt

Code: Select all

echo    - %DOW%'s "%BKdesc%" backup/verify failed.  Attempting to fix problems...

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: Display output of batch file on the screen and log file?

#10 Post by MKANET » 19 Apr 2012 18:11

It looks like I can use mtee afterall; but, not the way I originally wanted too. All I need to do is tack on the following text after each command:

Code: Select all

 | mtee/+ logfile.log

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Display output of batch file on the screen and log file?

#11 Post by Aacini » 19 Apr 2012 22:40

It is very easy to write a TEE application in Batch:

Code: Select all

@echo off
if exist "%~1" del "%~1"
:nextLine
   set line=
   set /P line=
   if not defined line goto endInput
   echo(%line%>> "%~1"
   echo(%line%
   goto nextLine
:endInput

The problem is that this Batch file have the usual problems of SET /P command and the special characters in the input line. The worst problem is that the input file will terminate at the first empty line, but perhaps it is enough for your needs...

Use this Batch file in the usual way:

Code: Select all

yourprogram | teebat filename.txt
However, pipelines in Windows/DOS may have some synchro problems, so it is possible that teebat program be locked in certain cases...

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Display output of batch file on the screen and log file?

#12 Post by Squashman » 20 Apr 2012 05:47

MKANET wrote:It sees like all the tee programs I've tried behave the same way when they see a CLS command. I've tried 5 different versions. :cry:

I guess I'm going to have to modify every single line in my batch file that echo's text to the console screen.

Could someone please tell me the easiest/most convenient possible way to make a small subroutine I can call to show a line being echoed (such as the below example) and send a copy of it to logfile.txt?

Here's an example of what I need echoed to the screen and a copy of it appended to logfile.txt

Code: Select all

echo    - %DOW%'s "%BKdesc%" backup/verify failed.  Attempting to fix problems...

I guess I would like to know how you were attempting to use all the different TEE programs.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Display output of batch file on the screen and log file?

#13 Post by Ed Dyreen » 21 Apr 2012 13:01

'

Code: Select all

@:: delayed insensitive, supports byVAL, eVAL

@echo off &setlocal disableDelayedExpansion

set $lf=^


::
%=       =%set "forQ_=for %%? in"

%=       =%set ^"$n1c=^^^%$lf%%$lf%^%$lf%%$lf%^^"
%=       =%set ^"$n2c=^^^^^^^%$lf%%$lf%^%$lf%%$lf%^^^%$lf%%$lf%^%$lf%%$lf%^^"

:: support: onDelayed, byVal¦byRef¦eVal, delayedChars, paramless input
:: (
%=       =%set ^"oMDelim_=%forQ_% (1,2) do if %%?==2 (%$n2c%
%=              =%set $=!$:^^^"=""!%=                                    pre-delay     =%%$n2c%
%=              =%set "$=!$:^=^^^^!"^^^&call set "$=%%^$:^!=#"#"^!%%"!%= delay 2-times =%%$n2c%
%=              =%set "$=!$:#"#"=^^^!"^^^&set $=!$:""=^^^"!%=            post-delay    =%%$n2c%
%=              =%set ?=^^^&%forQ_% (!$!) do set ?=!?!¦%%?%$n2c%
%=              =%if "!?!"=="" set ?=""%$n2c%
%=       =%)^^^&for /f "tokens=1-26delims=¦" %%a in ("!?!") do "
:: )


%=       =%set ^"tee_=%oMDelim_%(%$n1c%

%=              =%echo.%%~b^&echo.%%~b^>^>%%a%$n1c%

%=       =%endlocal)else setlocal enableDelayedExpansion^&set $="


( %tee_% file.LOG, ";Please <<preserve=^^this^!=line>>" )


pause
exit

Code: Select all

;Please <<preserve=^this!=line>>
Druk op een toets om door te gaan. . .


Hope it helps,

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Display output of batch file on the screen and log file?

#14 Post by foxidrive » 21 Apr 2012 15:55

You're a wizard, Ed.

My eyes glaze over when I see strings of ^^^^^ etc. :)

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: Display output of batch file on the screen and log file?

#15 Post by MKANET » 22 Apr 2012 18:59

Wow Ed, you really are a wizard. My eyes just hurt looking at those strings! It really helps!!! Thanks so much.

Post Reply