更改批处理文件日志的字体颜色 [英] Change font color for batch file log

查看:44
本文介绍了更改批处理文件日志的字体颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从批处理文件执行 exe 文件.这将运行 Windows 应用程序.每次我在应用程序中进行一些活动时,都会在批处理命令提示符中打印日志.我想用颜色突出显示日志中的特定文本,以便我可以查明后端活动.我该怎么做?

I am executing an exe file from a batch file. This runs a Windows application. Everytime I do some activity in the application, a log gets printed in the batch command prompt. I would like to highlight specific text in the log with colours so that I can pinpoint the backend activity. How do I do this?

我的批处理文件看起来像这样?

My batch file looks something like this?

cd C:\[Exe file location]
C:\[Exe File]
pause

推荐答案

您可以创建自己的 echo 命令.

You can create your own echo command.

将以下行放入桌面上名为 ColourText.bas 的文件中.

Put the following lines into a file called ColourText.bas on your desktop.

Imports System
Imports System.IO
Imports System.Runtime.InteropServices
Imports Microsoft.Win32

Public Module MyApplication  
Public Declare Function GetStdHandle Lib "kernel32" Alias "GetStdHandle" (ByVal nStdHandle As Long) As Long
Public Declare Function SetConsoleTextAttribute Lib "kernel32" Alias "SetConsoleTextAttribute" (ByVal hConsoleOutput As Long, ByVal wAttributes As Long) As Long
Public Const STD_ERROR_HANDLE = -12&
Public Const STD_INPUT_HANDLE = -10&
Public Const STD_OUTPUT_HANDLE = -11&

Sub Main()
    Dim hOut as Long
    Dim Ret as Long
    Dim Colour As Long
    Dim Colour1 As Long
    Dim Text As String
    hOut  = GetStdHandle(STD_OUTPUT_HANDLE)
    Colour = CLng("&h" & Split(Command(), " ")(0))
    Colour1 = Clng("&h" & Split(Command(), " ")(1))
    Text = Mid(Command(), 7)
    Ret = SetConsoleTextAttribute(hOut,  Colour)
    Console.Out.WriteLine(text)
    Ret = SetConsoleTextAttribute(hOut, Colour1)
End Sub
End Module

保存并在命令提示符中键入以下内容.

Save it and type the following in a command prompt.

"C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /target:exe /out:"%userprofile%\desktop\ColourText.exe" "%userprofile%\desktop\ColourText.bas" /verbose

一个名为 ColourText.exe 的文件将出现在您的桌​​面上.将其移至 Windows 文件夹.

A file called ColourText.exe will appear on your desktop. Move it to the Windows folder.

要使用,您必须使用两个字符代码来设置颜色,例如 01 而不是 1.

To use you must use two character codes to set colour eg 01 not 1.

ColourText ColourOfText ColourOfTextWhenFinished Text

EG 通过不传递任何文本在白色上设置蓝色,然后在白色文本上设置红色,最后在灰色上使用蓝色.

EG To set blue on white by not passing any text, then red on white text, finishing with blue on grey.

ColourText F1 F1
ColourText F2 71 This is green on white

ColourText F1 F1
cls
ColourText F4 F4
Echo Hello
Echo Hello today
ColourText F1 F1

CLS 命令也变得有趣.Color 不带参数的命令会将所有颜色重置为启动颜色.

Also the CLS command becomes interesting. Color command without parameters resets all colours to startup colours.

要获得颜色代码,请将以下数字相加.在程序员模式下使用计算器.这些是十六进制数字.它们可以加在一起,例如红色 + 蓝色 + FG 强度 = 13 = D.由于未使用 10+,背景将为黑色.颜色代码必须是两个字符,例如 08 而不是 8.

To get the colour code add the following numbers together. Use Calculator in programmers mode. These are hex numbers. They can be added together eg Red + Blue + FG Intensity = 13 = D. As 10+ wasn't used the background will be black. Colour codes MUST be two characters, eg 08 not 8.

FOREGROUND_RED = &H4     '  text color contains red.
FOREGROUND_INTENSITY = &H8     '  text color is intensified.
FOREGROUND_GREEN = &H2     '  text color contains green.
FOREGROUND_BLUE = &H1     '  text color contains blue.
BACKGROUND_BLUE = &H10    '  background color contains blue.
BACKGROUND_GREEN = &H20    '  background color contains green.
BACKGROUND_INTENSITY = &H80    '  background color is intensified.
BACKGROUND_RED = &H40    '  background color contains red.

这篇关于更改批处理文件日志的字体颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆