Batch File Logging Issues

2019-02-20 22:45发布

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 ?

2条回答
对你真心纯属浪费
2楼-- · 2019-02-20 23:14

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

查看更多
冷血范
3楼-- · 2019-02-20 23:23
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%
查看更多
登录 后发表回答