3

How can I save the out put of a batch file to a text file and the output should be displayed on console too.

For Eg:

@ECHO OFF
ECHO Hello World 
@pause

It will show Hello World on console

@ECHO OFF
ECHO Hello World >text.txt
@pause

It will save Hello World to text.txt

How can I make it both happen together?

Thanks in advance

user3647205
  • 63
  • 1
  • 1
  • 5
  • 3
    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) and [Displaying Windows command prompt output and redirecting it to a file](http://stackoverflow.com/questions/796476/displaying-windows-command-prompt-output-and-redirecting-it-to-a-file). – Mofi Jan 05 '16 at 17:16
  • Am new to console application. If my understanding is wrong please pardon. I checked the above link. By using the link i could direct all the logs to the same file. But while doing it no message is appearing on console. What i need is the console should show the messages and at the same time the messages that appearing on console should be logged in to a file. – user3647205 Jan 05 '16 at 19:24
  • @user3647205 You should very carefully read EVERYTHING of existing questions. [jeb](http://stackoverflow.com/users/463115/jeb) explained in [his answer](http://stackoverflow.com/a/4588136/3074564) that it is not possible to output a text to console window and at the same time redirect to a file using only Windows command processor. But jeb posted also a comment below his answer to JScript/Batch hybrid __tee__ written for this task. [rojo](http://stackoverflow.com/users/1683264/rojo) posted here the link to this solution once again. The other answers contain also various __tee__ solutions. – Mofi Jan 06 '16 at 09:27
  • http://stackoverflow.com/questions/796476/displaying-windows-command-prompt-output-and-redirecting-it-to-a-file does not satisfy my requirement. It is directing the console stdout and/or stderr to log file. But either of logging or watching in console only happens.http://stackoverflow.com/questions/11239924/windows-batch-tee-command guided to satisfy the both cases. But it is not consistant when i tried. Sometimes it will log and show the messages. but sometimes it is not for the same command. I dont know whats going – user3647205 Jan 07 '16 at 15:58
  • .My exact aim is to run a batch file from vba excel and wait till it finishes the execution and check the error log to see the execution was success or not before proceeding. And i want to see the console messages also. Any suggestion will be helpful. I am using Wscript to run the bat file from vba. – user3647205 Jan 07 '16 at 15:58
  • The duplicate `tee` command was already deleted. – amegyoushi Nov 18 '18 at 03:13

2 Answers2

0

Use 'FOR' command if you want: (yea this one only execute 1 command)

For /f "tokens=*" %%a in ('ECHO Hello world') do (
  echo %%a
  echo %%a >text.txt
)

But the solution below only useful if you want to send both output to console and file a command that show a lots of texts like 'SET' command.

So what about you create an external batch file then use it in your main file?
Example: Code for 'printBoth.bat'

@echo off
setlocal ENABLEDELAYEDEXPANSION
set string=
:loop
set string=!string!%1
shift
if not "%1"=="" goto loop

For /f "tokens=*" %%a in ('!string!') do (
  echo %%a
  echo %%a >text.txt
)

Now if you want to print to both console and file, just type this code: call printBoth.bat [type command in here]

an tran huu
  • 93
  • 2
  • 7
0

If you want to get the outputs of commands:

if exist "file.txt" del file.txt /f /q
for /f "delims=" %%k in ('command here') do (
  echo %%k
  echo %%k>>file.txt
)
  1. Note the >>. Don't use >. > would remove all the text in the file, then write the first output-ed line in the command, which is not good if there were multiple lines that will be "echo-ed" by FOR. >> creates a new line instead of replacing the line like >.
  2. DELIMS= works like TOKENS=*, but DELIMS= won't include the executed command.

I added if exist "file.txt" del file.txt /f /q, in order to not append the new output-ed lines. You can remove that if you want to append the lines to the file.

For customized ECHO outputs,

@echo off
echo TEXTHERE & echo TEXTHERE >>file.txt
echo TEXTHERE2 & echo TEXTHERE >>file.txt
rem ...and so on
  1. <command1> & <command2> means "do <command1>, then do <command2>"
  2. If you don't want to append, use > instead of >>.
amegyoushi
  • 524
  • 4
  • 14
  • 1
    `"tokens=*"` removes leading spaces. Therefore I would use `"delims="` instead. (watch the difference with a command like `dir`) See [here](https://stackoverflow.com/questions/15551379/how-do-i-make-a-log-of-all-echo-commands-in-a-batch-file/15553922#15553922) for some other ideas to do it. – Stephan Nov 17 '18 at 16:32
  • Now I'd use `delims=` instead, because the `dir` command will be included if I use `tokens=*`. – amegyoushi Nov 18 '18 at 03:08