Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
796 views
in Technique[技术] by (71.8m points)

windows - When adding a TextLog to Echo in Command Line, Input wont show up

I know when we add the >> after echo the output works but in the command line itself it's hidden. I'd like to know if there is a way I can do both without duplicating my code. So it should show in the command line and the output file.

Echo ### Backing up Drivers >> %Drive%:BackupBackup-Log.txt
Ping 127.0.0.1  >nul
xcopy "%HOMEDRIVE%drivers" /c /d /h /e /i /y "%Drive%:BackupDrivers"  >> %Drive%:BackupBackup-Log.txt

echo ### Backing up the Registry... >> %Drive%:BackupBackup-Log.txt
if not exist "%Drive%Registry" mkdir "%Drive%:BackupRegistry"
if exist "%Drive%Registry
egbackup.reg" Echo "Replacing %Drive%:BackupRegistry
egbackup.reg" >> %Drive%:BackupBackup-Log.txt
if exist "%Drive%Registry
egbackup.reg" del "%Drive%:BackupRegistry
egbackup.reg" >> %Drive%:BackupBackup-Log.txt
regedit /c /d /h /e /i /y  "%Drive%:BackupRegistry
egbackup.reg" >> %Drive%:BackupBackup-Log.txt
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is an explanation from an old answer of mine

for this purpose I use the following:

set LogFile=somepathlogfile.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%


You can find the complete answer here: How do I make a log of all ECHO commands in a BATCH file?

NOTES:

one & is enough, so instead of ^&^& write only ^&

The disadvantage is: it generates Disk-IO every time, you use it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...