Batch file 如何更改批处理文件中注释行的文本颜色

Batch file 如何更改批处理文件中注释行的文本颜色,batch-file,Batch File,我有一个批处理文件,如下所示: myfile.bat :: This is a sample batch file @echo off echo change directory to d: <---How to change color of only this line (comments lines) CD d:\ ... 没有内置的方法可以做到这一点。我建议您自己编写一个小助手程序,它可以更改文本的颜色属性,也可以编写一些具有特定颜色属性的文本 在C中,这可能如下所示: 使用制

我有一个批处理文件,如下所示:

myfile.bat
:: This is a sample batch file

@echo off
echo change directory to d: <---How to change color of only this line (comments lines)
CD d:\
...

没有内置的方法可以做到这一点。我建议您自己编写一个小助手程序,它可以更改文本的颜色属性,也可以编写一些具有特定颜色属性的文本

在C中,这可能如下所示:

使用制度; 类SetConsoleColor{ 静态环[]args{ 如果参数长度<3{ Console.Error.WriteLineUsage:SetConsoleColor[前台][后台][消息]; 回来 } Console.ForegroundColor=ConsoleColorEnum.ParsetypeofConsoleColor,args[0],true; Console.BackgroundColor=ConsoleColorEnum.ParsetypeofConsoleColor,args[1],true; 控制台。写搜索[2]; 控制台。重置颜色; } }
随意移植到C或其他你喜欢的语言;这是我在与一个50线C的怪物搏斗后最快的方法,它仍然不起作用;-

这是一个程序的源代码,可以实现您想要的功能:

我开始认为,如果没有额外的程序或对用户系统的修改,就不再有一种内置的方法来实现这一点


旁白-在我的场景中,如果需要修改用户的系统,我只会选择使用python、IronPython或JScript.NET。在这一问题发生6个月后,问了一个几乎相同的问题,jeb在那之后给出了一个很好的答案3:

他的答案允许在一行上打印多种颜色

这里是他的解决方案的一个改编版本,它是一个独立的批处理文件,可以作为一个实用工具批量打印彩色图像。打印Hello world!在白色背景上的红色文本中,您将使用call colorText f4 Hello world!。有关完整文档和限制,请参见代码中的注释

@echo off
:ColorText Color String
::
:: Prints String in color specified by Color.
::
::   Color should be 2 hex digits
::     The 1st digit specifies the background
::     The 2nd digit specifies the foreground
::     See COLOR /? for more help
::
::   String is the text to print. All quotes will be stripped.
::     The string cannot contain any of the following: * ? < > | : \ /
::     Also, any trailing . or <space> will be stripped.
::
::   The string is printed to the screen without issuing a <newline>,
::   so multiple colors can appear on one line. To terminate the line
::   without printing anything, use the ECHO( command.
::
setlocal
pushd %temp%
for /F "tokens=1 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
  <nul set/p"=%%a" >"%~2"
)
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1
popd
exit /b

您使用的是windows还是DOS ansi.sys允许将提示更改为各行的颜色?考虑不同的shell CygWin或脚本语言VBScript /JScript?是的,我正在使用DOS。随着批处理文件以不同的步骤进行,我在开始时给出了一个注释行,我希望这些注释行不同,以便区分。请您帮助我如何使用ansi.sysansi.sys仅适用于command.com。