ScrappyDev
ScrappyDev

Reputation: 2778

How to print the output of a batch script to a file w/out affecting how it displays on the script

I have a script that prints a lot of information, but also requires user input.

I need to be able to see the display in the command window, but also have it print the output on the screen to a log file.

-------------------------------
Info Here
-------------------------------
What is your favorite color? Blue

You sad your favorite color is "Blue".
Executing: AttackByBunny.exe

I'd prefer a way that can be set at the very top and apply to everything following.

SET ECHO OFF
SET FILEOUTPUT FILE.log
exec monty.exe

UPDATE for attempting w/ jtee.bat:
This doesn't seem to work for the script below. It just exits on the call mvn ... line. The pauses are just for debugging purposes.

UPDATED Script:

@echo off
set RELEASE_BRANCH=PROD_Release_7_0
echo Using NON_PROD_ENV/%RELEASE_BRANCH% | jtee.bat %RELEASE_BRANCH%.log
pause
if exist %RELEASE_BRANCH% (
    echo Running svn update %RELEASE_BRANCH% | jtee.bat %RELEASE_BRANCH%.log 1
    svn update %RELEASE_BRANCH% | jtee.bat %RELEASE_BRANCH%.log 1
) else (
    echo Running svn co https://svn_rep/branches/releases/NON_PROD_ENV/%RELEASE_BRANCH% | jtee.bat %RELEASE_BRANCH%.log 1
    svn co https://svn_rep/branches/releases/NON_PROD_ENV/%RELEASE_BRANCH% | jtee.bat %RELEASE_BRANCH%.log 1
)
pause
cd %RELEASE_BRANCH%
pause
call mvn clean release:clean release:prepare -DpreparationGoals="install" -DcompletionGoals="install versions:use-next-snapshots versions:commit" -Darguments='-Dmaven.test.skip=true' | jtee.bat ../%RELEASE_BRANCH%.log 1

pause

Upvotes: 2

Views: 700

Answers (2)

rojo
rojo

Reputation: 24466

Here's a full illustration of the .NET methods I mentioned in a comment above. This is a batch + PowerShell hybrid script (save it with a .bat extension) that will save the buffer contents of the console window in which it's run to a file called "buffer.log".

<# : Batch portion (PowerShell multi-line comment)
@echo off & setlocal

call :saveBuffer buffer.log
goto :EOF

:saveBuffer <outputfile>
set "logfile=%~f1"
powershell -noprofile -noninteractive "iex (${%~f0} | out-string)"
goto :EOF

: end batch / begin PowerShell hybrid chimera #>
$w = $Host.UI.RawUI.WindowSize.Width - 1
$h = $Host.UI.RawUI.CursorPosition.Y - 1
$rect = New-Object Management.Automation.Host.Rectangle 0, 0, $w, $h
$buffer = $Host.UI.RawUI.GetBufferContents($rect)
$lineBuilder = New-Object System.Text.StringBuilder
$out = New-Object System.Text.StringBuilder

# for older .NET compatibility
if (-not $lineBuilder.Clear) {
    $lineBuilder | Add-Member ScriptMethod Clear {$this.Length = 0}
}

foreach ($byte in $buffer) {
    [void]$lineBuilder.Append($byte.character)
    if (-not (++$x % ($w + 1))) {
        # End of line reached.  Append right-trimmed line to $out and start a new line.
        [void]$out.AppendLine($lineBuilder.ToString().TrimEnd())
        [void]$lineBuilder.Clear()
    }
}

# Write log file.  For UTF8, change encoding to utf8
$out.ToString() | out-file $env:logfile -encoding Default -width $w -force

Upvotes: 1

npocmaka
npocmaka

Reputation: 57242

Here you can find tee command for windows without external binaries. If you call the file jtee.bat you can use it like:

someCommand.exe arguments | jtee.bat FILE.log 1

the 1 to the end says to the script to append the log if the file already exists.

Upvotes: 1

Related Questions