0

My Batch file needs to write all the echos to %log% but some echos also need to be written into %someotherlog%. Is there any nice way than doing it separately? Here is how I currently do it:

echo this output needs to be written into two files >> %log%
echo this output needs to be written into two files >> %someotherlog%

I would prefer this to be one line of code:

echo There must be something like >> ( %log% | %someotherlog% )

Can you help me with the syntax?

Many thanks,

Peter

Peter Frey
  • 361
  • 4
  • 17
  • I tried: set text="Some text" && echo %text% >> %abstract% && echo %text% >> %log% but I have a formatting issue between the text and the first && – Peter Frey Jan 14 '20 at 10:30
  • try `set "text=Some text"` (note the position of the quotes). And a single `&` is sufficient. In general, what you need, is called `tee` (there are many such programs for Windows- use google). Or take a look at one of the solutions of [this question](https://stackoverflow.com/questions/15551379/how-do-i-make-a-log-of-all-echo-commands-in-a-batch-file), if you can accept some limitations. – Stephan Jan 14 '20 at 17:11
  • Hi Stephan, thanks for the help with the double quotes. I have read some article to tee already. It seems a little much for a simple "use same text twice"-task. Or did I read it wrong? My solution now is: set "text=Some Text." & echo !text! >> %abstract% & echo !text! >> %log% – Peter Frey Jan 17 '20 at 12:05
  • Personally, I would use a subroutine, like `call :write "Some Text" %log% %someotherlog%` to keep my main code clean and readable. Under the `:write` label, simply put three lines: `>>%2 echo %~1`, `>>%3 echo %~1` and `goto :eof` (if you are sure, the two files will ever be the same, hardcode them (`>>%log echo %~1` or even `>>file.log echo %~1`) and just `call :write "Some Text"` – Stephan Jan 17 '20 at 18:49
  • Hi Stefan, yes thank you. That makes the code even cleaner. – Peter Frey Jan 20 '20 at 09:21

0 Answers0