user2547886
user2547886

Reputation: 1

Batch File Logging Issues

how to get the logs of a batch file while getting the output on screen as well.

Note my batch file takes several input as well, I tried fair bit of things but its not working, does anyone have some simple solution ?

Upvotes: 0

Views: 214

Answers (2)

Stephan
Stephan

Reputation: 56180

set LogFile=somepath\logfile.txt
set logg=^> _^&^& type _^&^&type _^>^>%LogFile%
echo this goes to screen AND file! %logg%

This is a bit tricky. So let's disassemble that line to four parts:

set logg=      ^> _          ^&type _           ^&type _^>^>%LogFile%

The Idea is to print the line to a temporary file (named "_") (second part) then type the contents of that file to screen (third part) then type it to the logfile (fourth part).

Put that all to a variable (first part), so you don't have to type that monsterstring to every line. (this is the reason why the ">" and "&" are escaped with "^")

So every time you use

echo whatever %logg%

it will appear on the screen AND write to %logfile%

This also works with commands:

ipconfig %logg%

Upvotes: 0

David Ruhmann
David Ruhmann

Reputation: 11367

Use a TEE technique.

:Tee <Message> <File>
echo(%1
echo(%1>>%2
exit /b 0

Use like this

call :Tee "This is my Message to display in the log and on screen." "Output.txt"

Replace your echo commands with call :Tee commands

Upvotes: 1

Related Questions