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
fejese
  • 4,601
  • 4
  • 29
  • 36
Avrumi Sherman
  • 341
  • 2
  • 12
  • 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.` – foxidrive Jan 12 '14 at 07:11
  • Here's another thread with the same question -- http://stackoverflow.com/questions/796476/displaying-windows-command-prompt-output-and-redirecting-it-to-a-file – James L. Jan 12 '14 at 07:24
  • Thanks I guess i will delete this soon, since its a dubale. Thanks James and fox. – Avrumi Sherman Jan 12 '14 at 07: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? – Avrumi Sherman Jan 12 '14 at 08: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. – Avrumi Sherman Jan 12 '14 at 08:32
  • @AvrumiSherman: for explanation see my answer – Stephan Jan 12 '14 at 09:05
  • possible duplicate of [How do I echo and send console output to a file in a bat script?](http://stackoverflow.com/questions/503846/how-do-i-echo-and-send-console-output-to-a-file-in-a-bat-script) – fejese Sep 05 '15 at 18:21

1 Answers1

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.

Community
  • 1
  • 1
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • 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? – Avrumi Sherman Jan 12 '14 at 09:26
  • Wait let me play around with it. i got something but then it wont save it to log. – Avrumi Sherman Jan 12 '14 at 09:43
  • I got it I will post my answer. Thanks for your time. – Avrumi Sherman Jan 12 '14 at 10:01
  • 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 `_` – Stephan Jan 12 '14 at 10:57
  • If you'd like the temporary file to be automatically cleaned up, add `^&^& DEL _` to the end of the `logg` variable. – ErikusMaximus Aug 11 '17 at 16:16
  • deleting the file `_` each time costs an additional disk access and so slows it down. If needed, delete it once at the end of the script. – Stephan Jun 16 '19 at 11:10