如何在 Windows 批处理文件中拥有多种颜色?
我想知道 Windows 批处理文件中的同一行是否可以有不同颜色的文本,例如,如果它说
echo hi world
我希望“hi”为一种颜色,“world”为另一种颜色。也许我可以将 COLOR 命令设置为变量:
set color1= color 2
set color9= color A
然后将它们部署在同一行上,
echo hi world
但我不知道该怎么做。
I was wondering if its possible to have different colored text on the same line in a Windows batch file, for example if it says
echo hi world
I want "hi" to be one color, and "world" to be another color. Maybe I could set the COLOR command as a variable:
set color1= color 2
set color9= color A
and then deploy them both on the same line along with
echo hi world
but I don't know how I would do that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
您无需任何外部程序即可进行多色输出。
它使用 findstr 命令的颜色功能。
Findstr 可以配置为以定义的颜色输出行号或文件名。
因此,我首先创建一个以文本作为文件名的文件,内容是单个
字符 (ASCII 8)。然后我搜索文件和 nul 中的所有非空行,因此文件名将以正确的颜色输出并附加冒号,但冒号会立即被
删除。编辑:一年后...所有字符都有效
这使用有效路径/文件名的规则。
如果
\..\
位于路径中,则前缀元素将被完全删除,并且该元素不必仅包含有效的文件名字符。You can do multicolor outputs without any external programs.
It uses the color feature of the findstr command.
Findstr can be configured to output line numbers or filenames in a defined color.
So I first create a file with the text as filename, and the content is a single
<backspace>
character (ASCII 8).Then I search all non empty lines in the file and in nul, so the filename will be output in the correct color appended with a colon, but the colon is immediatly removed by the
<backspace>
.EDIT: One year later ... all characters are valid
This uses the rule for valid path/filenames.
If a
\..\
is in the path the prefixed elemet will be removed completly and it's not necessary that this element contains only valid filename characters.jeb 编辑后的答案接近解决所有问题。但它在处理以下字符串时存在问题:
我已经将他的技术修改为我认为可以真正处理任何可打印字符的字符串,除了长度限制。
其他改进:
使用临时文件的 %TEMP% 位置,因此不再需要对当前目录的写访问权限。
创建了 2 个变体,一个采用字符串文字,另一个采用包含该字符串的变量的名称。变量版本通常不太方便,但它消除了一些特殊字符转义问题。
添加了 /n 选项作为可选的第三个参数,以在输出末尾附加换行符。
退格键无法跨越换行符,因此如果换行,该技术可能会出现问题。例如,如果控制台的行宽为 80,则打印长度在 74 - 79 之间的字符串将无法正常工作。
更新 2012-11-27
< em>此方法在 XP 上失败,因为 FINDSTR 在屏幕上将退格键显示为句点。 jeb 的原始答案适用于 XP,尽管已经注意到了限制
更新 2012-12-14
在 < a href="http://www.dostips.com/forum/viewtopic.php?f=3&t=4014" rel="noreferrer">DosTips 和 SS64。事实证明,如果在命令行上提供,FINDSTR 也会损坏包含扩展 ASCII 的文件名。我更新了我的 FINDSTR 问答。
下面是在 XP 上运行的版本,支持除 0x00 (nul)、0x0A(换行符)和 0x0D(回车符)之外的所有单字节字符。然而,在 XP 上运行时,大多数控制字符将显示为点。这是XP上FINDSTR的固有特征,无法避免。
不幸的是,添加对 XP 和扩展 ASCII 字符的支持会减慢例程:-(
只是为了好玩,我从 joan stark 的 ASCII Art Gallery 并将其改编为与 ColorPrint 一起使用。我添加了一个 :c 入口点只是为了速记,并处理引用文字的问题。
jeb's edited answer comes close to solving all the issues. But it has problems with the following strings:
I've modified his technique to something that I think can truly handle any string of printable characters, except for length limitations.
Other improvements:
Uses the %TEMP% location for the temp file, so no longer need write access to the current directory.
Created 2 variants, one takes a string literal, the other the name of a variable containing the string. The variable version is generally less convenient, but it eliminates some special character escape issues.
Added the /n option as an optional 3rd parameter to append a newline at the end of the output.
Backspace does not work across a line break, so the technique can have problems if the line wraps. For example, printing a string with length between 74 - 79 will not work properly if the console has a line width of 80.
UPDATE 2012-11-27
This method fails on XP because FINDSTR displays backspace as a period on the screen. jeb's original answer works on XP, albeit with the limitations already noted
UPDATE 2012-12-14
There has been a lot of development activity at DosTips and SS64. It turns out that FINDSTR also corrupts file names containing extended ASCII if supplied on the command line. I've updated my FINDSTR Q&A.
Below is a version that works on XP and supports ALL single byte characters except 0x00 (nul), 0x0A (linefeed), and 0x0D (carriage return). However, when running on XP, most control characters will display as dots. This is an inherent feature of FINDSTR on XP that cannot be avoided.
Unfortunately, adding support for XP and for extended ASCII characters slows the routine down :-(
Just for fun, I grabbed some color ASCII art from joan stark's ASCII Art Gallery and adapted it for use with ColorPrint. I added a :c entry point just for shorthand, and to handle an issue with quote literals.
实际上,这可以在不创建临时文件的情况下完成。
jeb 和 dbenham 描述的方法即使对于不包含退格键的目标文件也适用。关键点是 findstr.exe 识别的行不能以 CRLF 结尾。
因此,使用不以 CRLF 结尾的行扫描的明显文本文件是调用批处理本身,前提是我们以这样的行结束它!
这是一个以这种方式工作的更新的示例脚本...
与上一个示例的更改:
PS。我的输出有问题!您在上一个示例中没有的字符。 (或者至少你没有出现相同的症状。)有待调查。
Actually this can be done without creating a temporary file.
The method described by jeb and dbenham will work even with a target file that contains no backspaces. The critical point is that the line recognized by findstr.exe must not end with a CRLF.
So the obvious text file to scan with a line not ending with a CRLF is the invoking batch itself, provided that we end it with such a line!
Here's an updated example script working this way...
Changes from the previous example:
PS. I'm having a problem with the output of the ! character that you did not have in the previous example. (Or at least you did not have the same symptoms.) To be investigated.
如果您有现代 Windows(安装了 powershell),则以下操作也可以正常工作
根据您的需要调整颜色。
If you have a modern Windows (that has powershell installed), the following may work fine as well
Adjust the color as you see fit.
将 dbenham 的鸟和语法 与 结合起来skrebbel 的 powershell
write-host
方法,看起来 powershell 可以比 dbenham 的纯批处理方法更快地渲染复杂的艺术(好吧,无论如何,在 powershell 启动一次之后)。尽管我没有用鸟以外的任何东西进行过测试,但需要对琴弦进行最少的按摩。例如,如果您想要亮绿色的传输结束字符,那么您可能会不走运。 :)此方法需要回显到临时文件,只是因为为每个
call :c
调用 powershell 需要很长时间,而且速度要快得多对一次 powershell 调用的输出进行排队。但它确实具有简单和高效的优点。结果:
Combining dbenham's bird and syntax with skrebbel's powershell
write-host
method, it seems that powershell can render complex art more quickly than dbenham's pure batch method (well, after powershell has been primed once, anyway). Minimal massaging of the strings are needed, although I haven't tested this with anything other than the bird. If you want a bright green end-of-transmission character for example, you may be out of luck. :)This method requires echoing out to a temp file, simply because invoking powershell for each
call :c
takes forever, and it's much faster to queue the output for one powershell invocation. But it does have the advantage of simplicity and efficiency.Result:
无需外部工具。这是一个自我-编译的bat/.net混合(应保存为
.BAT
),可以在任何安装了.net框架的系统上使用(很少见到即使对于最旧的 XP/2003 安装,也没有 .NET 框架的 Windows)。它使用 jscript.net 编译器创建一个能够仅针对当前行打印具有不同背景/前景色的字符串的 exe。示例 coloroutput.bat -s "aa\nbb\n\u0025cc" -b 10 -f 3 -n -e
您还可以检查 Carlos 的颜色函数 -> http://www.dostips.com/forum/viewtopic.php ?f=3&t=4453
Without external tools.This is a self-compiled bat/.net hybrid (should be saved as
.BAT
) that can be used on any system that have installed .net framework (it's a rare thing to see an windows without .NET framework even for the oldest XP/2003 installations) . It uses jscript.net compiler to create an exe capable to print strings with different background/foreground color only for the current line.Example
coloroutput.bat -s "aa\nbb\n\u0025cc" -b 10 -f 3 -n -e
You can also check carlos' color function -> http://www.dostips.com/forum/viewtopic.php?f=3&t=4453
是的,可以使用 cmdcolor:
hi
将为深绿色,world
- 浅绿色。Yes, it is possible with cmdcolor:
hi
will be dark green, andworld
- light green.Windows 10((版本 1511,内部版本 10586,发布 2015-11-10))支持 ANSI 颜色< /a>.
您可以使用退出键来触发颜色代码。
在命令提示符中:
echo Ctrl+[
[32m HI
Ctrl+[[0m
Enter使用文本编辑器时,可以使用 ALT 键代码。
ESC 键代码可以使用 ALT 和 NUMPAD 数字创建:Alt+
027
Windows 10 ((Version 1511, build 10586, release 2015-11-10)) supports ANSI colors.
You can use the escape key to trigger the color codes.
In the Command Prompt:
echo Ctrl+[
[32m HI
Ctrl+[[0m
EnterWhen using a text editor, you can use ALT key codes.
The ESC key code can be created using ALT and NUMPAD numbers : Alt+
027
对于那些不想阅读一堆解释的人,我会先给你想要的答案。
使用 ANSI_escape_code.SGR_parameters
I'll give you the answer you're looking for first, for those who don't want to read a bunch of explanations.
use ANSI_escape_code.SGR_parameters
I suggest you copy (in case you are missing the ESC key) the above contents, and then paste it on the notepad++ or others IDE you like.
output as below picture
Explanation
according ANSI_escape_code.SGR_parameters you know
ESC[0m
orESC[m
ESC[1m
ESC[3m
ESC[4m
ESC[8m
ESC[31m
ESC[38m;2;255;0;0m
ESC[48m;2;255;0;0m
Of course, there are many more you can go to see.
for example,
ESC[31mESC[4mfore color: red and style:underlineESC[0m
echo [31m[4mfore color: red and style:underline[0m
where ESC = 0x1B (since typing ESC in here, the user can't see it.)
Script and Exercises
output:
中涵盖了几种方法
“51}如何在 NT 脚本中以不同颜色回显线条?”
http://www.netikka.net/tsneti/info/tscmd051.htm
替代方案之一:如果您能掌握 QBASIC,那么使用颜色相对容易:
Several methods are covered in
"51} How can I echo lines in different colors in NT scripts?"
http://www.netikka.net/tsneti/info/tscmd051.htm
One of the alternatives: If you can get hold of QBASIC, using colors is relatively easy:
如果您的控制台支持 ANSI 颜色代码(例如 ConEmu、Clink 或 ANSICON)你可以这样做:
其中 ESC 变量包含 ASCII 字符 27。
我找到了一种在此处填充 ESC 变量的方法: http://www.dostips.com/forum/viewtopic.php?p=6827#p6827
使用
tasklist
可以测试哪些DLL被加载到进程中。以下脚本获取运行该脚本的 cmd.exe 的进程 ID。检查它是否具有将添加注入的 ANSI 支持的 dll,然后根据是否支持颜色将颜色变量设置为包含转义序列或为空或不。
If your console supports ANSI colour codes (e.g. ConEmu, Clink or ANSICON) you can do this:
where ESC variable contains ASCII character 27.
I found a way to populate the ESC variable here: http://www.dostips.com/forum/viewtopic.php?p=6827#p6827
and using
tasklist
it's possible to test what DLLs are loaded into a process.The following script gets the process ID of the cmd.exe that the script is running in. Checks if it has a dll that will add ANSI support injected, and then sets colour variables to contain escape sequences or be empty depending on whether colour is supported or not.
您应该从以下位置下载 chgcolor.zip
http://www.mailsend-online .com/blog/setting-text-color-in-a-batch-file.html
并从以下位置下载 echoj.zip
www.mailsend-online.com/blog/?p=41
它们都位于页面底部。
将两个文件夹解压到桌面
并将可执行文件(.exe 文件)从提取的文件夹中复制到 C:\Windows 目录。这将允许它们从命令行执行。
打开记事本并将以下内容复制到其中:
将文件保存到桌面作为 hi.bat。现在打开命令提示符并导航到桌面文件夹并键入不带引号的“hi.bat”。这应该可以帮助您开始,请务必阅读这两个网页以获得完整的教程。
You should download chgcolor.zip from
http://www.mailsend-online.com/blog/setting-text-color-in-a-batch-file.html
and also download echoj.zip from
www.mailsend-online.com/blog/?p=41
They're both towards the bottom of the page.
Extract both folders to the desktop
and copy the executables(.exe files) from inside the extracted folders to the C:\Windows directory. This will allow them to be executed from the command line.
Open up notepad and copy the following into it:
Save the file to the Desktop as hi.bat. Now open the command prompt and navigate to your Desktop folder and type "hi.bat" without the quotes. That should get you started besure to read both webpages to get a full tutorial.
我喜欢@jeb发布的函数方法,但他的(第二个)解决方案有一些小问题:
这就是为什么我想分享我对这些问题的解决方案,而不是请求侵入性编辑。
与@jeb one相比,对该脚本的评论:
I liked the function approach posted by @jeb but there are a few small issues with his (second) solution:
That is why I feel to share my solution to these issues, insted of requesting an invasive edit.
Comments to this script in comparison to @jeb one:
虚拟终端代码使用的宏解决方案对于 Windows 10 用户
对于 Windows 10 用户,这是除了直接使用 VT 序列之外最快的方法,同时更具可读性。
A macro solution for virtual terminal code usage for windows 10 users
For windows 10 users, it is the fastest method outside of using VT sequences directly while of being more readable.