为了账号安全,请及时绑定邮箱和手机立即绑定

如何在Windows批处理文件中有多种颜色?

如何在Windows批处理文件中有多种颜色?

回首忆惘然 2019-06-18 11:08:46
如何在Windows批处理文件中有多种颜色?我想知道是否有可能在Windows批处理文件中的同一行中有不同颜色的文本,例如,如果它说echo hi world我希望“嗨”是一种颜色,“世界”是另一种颜色。也许我可以将color命令设置为变量:set color1= color 2 set color9= color A然后将它们部署在同一条线上,同时echo hi world但我不知道我该怎么做。
查看完整描述

3 回答

?
LEATH

TA贡献1936条经验 获得超6个赞

您可以在没有任何外部程序的情况下进行多色输出。

@echo off
SETLOCAL EnableDelayedExpansion
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
  set "DEL=%%a"
)
echo say the name of the colors, don't read

call :ColorText 0a "blue"
call :ColorText 0C "green"
call :ColorText 0b "red"
echo(
call :ColorText 19 "yellow"
call :ColorText 2F "black"
call :ColorText 4e "white"

goto :eof

:ColorText
echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1
goto :eof

它使用findstr命令的颜色特性。

Findstr可以配置为以定义的颜色输出行号或文件名。
因此,我首先创建一个以文本为文件名的文件,内容是一个<backspace>性格(ASCII 8)。
然后,我在文件和nul中搜索所有非空行,因此文件名将以附加冒号的正确颜色输出,但冒号将由<backspace>.

所有字符都是有效的。

@echo off
setlocal EnableDelayedExpansion
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
  set "DEL=%%a"
)

rem Prepare a file "X" with only one dot
<nul > X set /p ".=."

call :color 1a "a"
call :color 1b "b"
call :color 1c "^!<>&| %%%%"*?"
exit /b

:color
set "param=^%~2" !
set "param=!param:"=\"!"
findstr /p /A:%1 "." "!param!\..\X" nul
<nul set /p ".=%DEL%%DEL%%DEL%%DEL%%DEL%%DEL%%DEL%"
exit /b

这使用了有效路径/文件名的规则。
如果\..\在路径中,前缀元素将被完全删除,并且不需要此元素只包含有效的文件名字符。


查看完整回答
反对 回复 2019-06-18
?
收到一只叮咚

TA贡献1821条经验 获得超4个赞

如果您有一个现代Windows(安装了Powershell),那么下面的操作也会很好

call :PrintBright Something Something

  (do actual batch stuff here)

call :PrintBright Done!
goto :eof


:PrintBright
powershell -Command Write-Host "%*" -foreground "White"

根据你认为合适的情况调整颜色。


查看完整回答
反对 回复 2019-06-18
  • 3 回答
  • 0 关注
  • 1782 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信