1

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%:\Backup\Backup-Log.txt
Ping 127.0.0.1  >nul
xcopy "%HOMEDRIVE%\drivers" /c /d /h /e /i /y "%Drive%:\Backup\Drivers"  >> %Drive%:\Backup\Backup-Log.txt

echo ### Backing up the Registry... >> %Drive%:\Backup\Backup-Log.txt
if not exist "%Drive%\Registry" mkdir "%Drive%:\Backup\Registry"
if exist "%Drive%\Registry\regbackup.reg" Echo "Replacing %Drive%:\Backup\Registry\regbackup.reg" >> %Drive%:\Backup\Backup-Log.txt
if exist "%Drive%\Registry\regbackup.reg" del "%Drive%:\Backup\Registry\regbackup.reg" >> %Drive%:\Backup\Backup-Log.txt
regedit /c /d /h /e /i /y  "%Drive%:\Backup\Registry\regbackup.reg" >> %Drive%:\Backup\Backup-Log.txt
7
  • Use a TEE filter that works in Windows NT line. Swiss File Knife is one tool which has it. SFK - The Swiss File Knife File Tree Processor. Commented Jan 12, 2014 at 7:11
  • Here's another thread with the same question -- stackoverflow.com/questions/796476/… Commented Jan 12, 2014 at 7:24
  • Thanks I guess i will delete this soon, since its a dubale. Thanks James and fox. Commented Jan 12, 2014 at 7:40
  • Sorry i cant comment over there since i need 50 rep so i am gonna comment here. i found this, but how do i use it? ECHO Print line to screen and log to file. >_ && type _ && type _ >> logfile.txt what dose the _ && type _ && type _ mean? Commented Jan 12, 2014 at 8:16
  • Thats on old Post i think back in 2009. those answers don't work for me. i dont want to download a program since i am using this for other 100 computers. it might of work in xp but not in 7. Commented Jan 12, 2014 at 8:32

1 Answer 1

3

Here is an explanation from an old answer of mine

for this purpose I use the following:

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%


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.

Sign up to request clarification or add additional context in comments.

6 Comments

I am getting access is denied Using windows 7. Thats what happened to me before, i guess i thute there was a problem with the script. Any clues on how to run this batch as administrator?
Wait let me play around with it. i got something but then it wont save it to log.
I got it I will post my answer. Thanks for your time.
the "Access denied" problem propbably is: the temporary file (named _) will be put into the current directory, which you may not have wirte-permission. Workarounds: a) cd /d %~dp0 at the beginning of the script b) name the tempfile with a path to a dir witch you have permissions for example: %temp%\_ instead of _
If you'd like the temporary file to be automatically cleaned up, add ^&^& DEL _ to the end of the logg variable.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.