2016-12-30 31 views
0

我正在执行批处理文件中的exe文件。这运行一个Windows应用程序。每次我在应用程序中执行一些活动时,都会在批处理命令提示符中打印日志。我想用颜色突出显示日志中的特定文本,以便我可以精确定位后端活动。我该怎么做呢?更改批处理文件日志的字体颜色

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

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

的可能的复制(http://stackoverflow.com/questions/2048509/how-to-echo-with-different-colors-in-the [如何在Windows命令行不同的颜色呼应] -windows-command-line) –

+1

看看这个==> http://stackoverflow.com/questions/41309768/batch-file-to-run-ping-command-and-output-to-text-aswell/ 41316112#41316112 – Hackoo

回答

0

您可以创建自己的回显命令。

把下面的行到一个名为您的桌面上ColourText.bas文件。

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 

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

"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文件夹

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

ColourText ColourOfText ColourOfTextWhenFinished Text 

EG若要通过不通过任何文本,然后白字红色,蓝色灰色完成设置白色蓝色。

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不带参数的命令会将所有颜色重置为启动颜色。

要获取颜色代码,请将以下数字一起添加。在程序员模式下使用计算器。这些是十六进制数字。它们可以加在一起,例如Red + Blue + FG Intensity = 13 = D.由于没有使用10+,所以背景会变黑。颜色代码必须是两个字符,例如08而不是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.