2

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
ScrappyDev
  • 2,307
  • 8
  • 40
  • 60
  • 1
    You'd either have to have two `echo` statements for each line of output -- one for the console and one redirected to a log file; or use either [GnuWin32 `tee`](http://gnuwin32.sourceforge.net/packages/coreutils.htm) or a [script that simulates tee](http://stackoverflow.com/a/10719322/1683264). And even then, when you `set /P` you'll still have to `echo` the line again to the log file. Or you could use PowerShell and .NET methods to save the buffer -- something like `$Host.UI.RawUI.GetBufferContents([Management.Automation.Host.Rectangle]$rectangleObj)`. – rojo Mar 12 '16 at 02:29
  • The script I'm updating is executing another exe that has a lot of output & input. So it's not me manually puting input. – ScrappyDev Mar 15 '16 at 20:32
  • I added an answer with an illustration of what I was talking about. It won't log to a file in realtime, but after your exe with a lot of output and input is finished and exited, it can save the contents of the console window as a log file. – rojo Mar 16 '16 at 04:53
  • Scrappy, does either of the solutions below solve your problem? – rojo Mar 29 '16 at 13:50
  • I haven't had a chance to try them out. It is on my todo list. – ScrappyDev Mar 29 '16 at 15:51

2 Answers2

1

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
rojo
  • 24,000
  • 5
  • 55
  • 101
  • Will this work for all lengths of output, or only short outputs? – ScrappyDev Mar 16 '16 at 13:31
  • Try it and see what happens. As long as the content you want to capture is still either in view or in the scrollback buffer, it should be captured. If your console window only has a 300-line buffer and you have 500 lines of output, you'll have to tweak the settings of your console window. – rojo Mar 16 '16 at 13:47
1

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.

Community
  • 1
  • 1
npocmaka
  • 55,367
  • 18
  • 148
  • 187