Echo into multiple files in Batch script

≡放荡痞女 提交于 2019-12-24 00:48:30

问题


How can I echo a line into multiple files in a single command? Example: echo Hello World! into file1.log and file2.log

EDIT: Windows-XP Batch Script

CHALLENGE: Give me a one-liner =D. If it can't be done w/one line I'm not interested.

My solution was

ECHO Hello World!>tmp.log & COPY file1.log + tmp.log & COPY file2.log + tmp.log

But I'm hoping for one that is a single command and not multiple commands.


回答1:


If you need it only for single lines (of input) you can write your own tee with batch.

@ECHO OFF
rem *** singleLineTee destination1 destination2
setlocal EnableDelayedExpansion
set /p var=
> %1 echo(!var!
> %2 echo(!var!

And use it with echo hello | singleLineTee file1.log file2.log

EDIT: The same as an one liner

echo hello | ( cmd /v:on /c "set /p var=& >> file1.log echo !var!& >> file2.log echo !var!")

The cmd /v:on /c is necessary to enable the delayed expansion




回答2:


echo "Hello World" | tee file1.log file2.log



回答3:


You could do something similar to jeb, but keep it in the same batch file as your calling code

@ECHO OFF
    del tem1.txt
    del tem2.txt
    call :SingleLineTeeSub "echo Hello" Tem1.txt Tem2.txt
    call :SingleLineTeeSub "echo World" Tem1.txt Tem2.txt
    call :SingleLineTeeSub "Dir tem1.txt" Tem1.txt Tem2.txt
goto :eof

:SingleLineTeeSub
    rem *** call :SingleLineTeeSub "command [params]" destination1 destination2
    setlocal EnableDelayedExpansion
    set theCmd=%~1
    >> %2 %theCmd%
    >> %3 %theCmd%
goto :eof



回答4:


Will this suit your requirement:

(echo hello > file1) && copy file1 file2


来源:https://stackoverflow.com/questions/7649769/echo-into-multiple-files-in-batch-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!