如何改变意见行的文本颜色在批处理文件 [英] How to change the text color of comments line in a batch file

查看:599
本文介绍了如何改变意见行的文本颜色在批处理文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  myfile.bat
::这是一个示例批处理文件关闭@echo
回声改变目录到d:< ---如何改变只此行的颜色(注释行)
CD D:\\
...


解决方案

一个几乎相同的问题被问这个后6个月,杰布,经过提供一个很好的答案3:的如何在一个批处理文件,多种颜色?

他的回答让在一行上打印多颜色!

下面是他作为可以用作一个实用程序来进行彩色打印在批次独立批处理文件溶液的适应。要打印世界,你好!红色文本在白色背景上,你会使用调用colorText F4世界,你好!。请参见code注释的完整文档和限制。

 关闭@echo
:ColorText颜色字符串
::
::打印字符串由颜色指定的颜色。
::
::颜色应该是2进制数字
::第1位指定背景
::第二位指定前景
::看颜色/?如需更多帮助
::
:: string的是打印文本。所有的报价将被剥离。
::字符串不能包含任何以下内容:*? < > | :\\ /
::此外,任何尾随。或lt;空>将被剥离。
::
::字符串被打印在屏幕上,而不会发出<换行符>中
::所以多种颜色可以出现在一行中。要终止该行
::不打印任何东西,使用echo(命令。
::
SETLOCAL
PUSHD%TEMP%
FOR / F标记= 1 delims =#%%一个在('提示#$ H#$ E#&安培; echo开&安培;在(1)做REM %% B')做(
  < NUL集/ P= %%一个>中%〜2

FINDSTR / V / A:%1 / R^ $%〜2NUL
德尔%〜2> NUL 2 - ;&放大器; 1
POPD
退出/ B

I have a batch file as follows:

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:\
...

解决方案

A nearly identical question was asked 6 months after this one, and jeb provided a good answer 3 after that: how to have multiple colors in a batch file?

His answer allows printing multiple colors on a single line!

Here is an adaptation of his solution as a standalone batch file that can be used as a utility to print in color in batch. To print Hello world! in red text on a white background you would use call colorText f4 "Hello world!". See the comments in the code for full documentation and limitations.

@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

这篇关于如何改变意见行的文本颜色在批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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