我正在运行一个批处理文件,并将std输出重定向到文件,并且我想在控制台上显示的输出中获取彩色文本。我找到了可以显示颜色文本的内容,但是我不知道如何将其发送到控制台。这有效,但是当stdout重定向时不起作用: How to have multiple colors in a Windows batch file?
我这样称呼批处理文件:
mybatch> myoutput.txt
我的批处理文件如下:
:: 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
如何获取该文本以转到控制台?
答案 0 :(得分:0)
您可以考虑从批处理文件中使用FindStr
,而不是使用PowerShell
:
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"
可以使用别名将其缩短一点:
PowerShell -NoP "'This is written to a file and also printed to the console in blue'|AC 'output.log' -Pas|Write-Host -F Blue"
您甚至可以同时指定背景颜色[-BackgroundColor
| -B
]:
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"
允许的颜色为Black
,DarkBlue
,DarkGreen
,DarkCyan
,DarkRed
,DarkMagenta
,{{1} },DarkYellow
,Gray
,DarkGray
,Blue
,Green
,Cyan
,Red
,Magenta
和Yellow