-1

I am running a batch file with std output being redirected to a file and I want to get color text in the output that is being displayed on the console. I have found something that gives the color text, but I can't figure out how to send it to the console. This works, but not when stdout is redirected: How to have multiple colors in a Windows batch file?

I call the batch file like this:
mybatch >myoutput.txt

My batch file looks like this:

:: Sample Batch file
@echo off
@echo this goes to the text file when std output is redirected
@echo this goes to the console >CON
call :ColorText 19 "This uses the above referenced method to print in blue, but doesn't go to the console" & echo( 
.
.
.
:ColorText
    <nul set /p ".=%DEL%" > "%~2"
    findstr /v /a:%1 /R "^$" "%~2" nul
    del "%~2" > nul 2>&1
    goto :eof

How can I get that text to go to the console?

Squashman
  • 13,649
  • 5
  • 27
  • 36
cashonly
  • 119
  • 9

1 Answers1

0

Instead of using FindStr, you could consider using PowerShell from your batch file:

PowerShell -NoProfile "'This is written to a file and also printed to the console in blue'|Add-Content 'output.log' -PassThru|Write-Host -ForegroundColor Blue"

It can be shortened a little by using aliases:

PowerShell -NoP "'This is written to a file and also printed to the console in blue'|AC 'output.log' -Pas|Write-Host -F Blue"

You can even specify the background color, [-BackgroundColor|-B] at the same time:

PowerShell -NoP "'This is written to a file and also printed to the console in blue with a yellow background'|AC 'output.log' -Pas|Write-Host -F Blue -B Yellow"

The allowed colors are Black, DarkBlue, DarkGreen, DarkCyan, DarkRed, DarkMagenta, DarkYellow, Gray, DarkGray, Blue, Green, Cyan, Red, Magenta, Yellow and White

Compo
  • 36,585
  • 5
  • 27
  • 39