问题
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