Reputation: 547
Thanks for looking into my question.
I have a batch command running in jenkins windows batch command prompt. Let's a take a simple command
echo hello world
I want to write this command output to a file as well as this output to be displayed on command prompt. I have tried below 2 ways.
echo helloworld |tee -a .\test.log
echo hello world >>.\test.log >>CON
both of the above are not working.
Could anyone help me with this.
Thanks, R Dama.
Upvotes: 1
Views: 13909
Reputation: 34899
There is no tee
command in pure batch scripting.
The easiest work-around is to redirect to a file and then to type the file, like this (do not put a SPACE behind the echo
command as it was output also):
echo Hello world!> "logfile.txt"
type "logfile.txt"
Of course, the output is only printed to the console as soon as the command (echo
in the example) has been finished executing.
If you want to append to an already existing log file logfile.txt
, use this instead:
echo Hello world!> "logfile.tmp"
type "logfile.tmp"
type "logfile.tmp" >> "logfile.txt"
del "logfile.tmp"
Alternatively, in case logfile.txt
does for sure exist, you may use this:
echo Hello world!> "logfile.tmp"
type "logfile.tmp"
copy /Y "logfile.txt" + "logfile.tmp" "logfile.txt" /B > nul
del "logfile.tmp"
Upvotes: 2
Reputation: 16226
Windows does not really have a tee
command short of the PowerShell Tee-Object
(alias tee
). Here is a cmd script subroutine that will produce a tee
effect. The DoMsg
subroutine will append everything to the log file and then write it to stdout.
@ECHO OFF
CALL:DoMsg ECHO hello, world
CALL:DoCmd DIR
EXIT /B 0
:DoMsg
ECHO %* >"%TEMP%\domsg.tmp"
TYPE "%TEMP%\domsg.tmp" >>test.log
TYPE "%TEMP%\domsg.tmp"
GOTO:EOF
:DoCmd
CALL:DoMsg CMD: %*
%*
GOTO:EOF
Upvotes: 1
Reputation: 736
If you don't need it to do both in real time, you could use 2 separate commands:
echo hello world > test.log
type test.log
Which could also be done in a single line:
echo hello world > test.log & type test.log
Upvotes: 1
Reputation: 11
There is a way to do this with PowerShell:
powershell "echo HelloWorld | tee test.txt"
Another way, without PowerShell, could be to type the file that received the output:
echo HelloWorld > test.txt | type test.txt
I do prefer the first solution...
Upvotes: 1