bat echo 输出不同的文字颜色和背景色,命令行显示彩色字符

bat显示多行文字,逐个显示哦!不同的颜色!

最近想修改bat文件输出提示的时候能有不同的颜色提示,在网上找了下,发现这个文章,实现的不错,先记录下来,留着后面研究。

这是曾经写的,又稍微改进了一下。
@echo off
set str=青天有月来几时 我今停杯一问之 人攀明月不可得 月行却与人相随
setlocal enabledelayedexpansion
set col=CAD9B
for /f "tokens=1-4 delims= " %%1 in ("%str%") do (echo.
for %%m in (%%1 %%2 %%3 %%4) do (set s=%%m
for /l %%a in (0,1,6) do (
call set b=0%%col:~!x!,1%%
set /a x+=1&if !x!==5 (set x=0)
set c=!s:~%%a,1!
set/p= <nul>!c! 
findstr /a:!b! .* "!c!*"
del !c!
ping /n 1 /w 500 127.1>nul&ping /n 1 /w 500 127.1>nul&ping /n 1 /w 500 127.1>nul)
echo.&echo.))
pause>nul&exit

出处:http://zhidao.baidu.com/link?url=ZXgHX0ON0ESo2j_RlUOKyDHPeBFmBMY1gk1FgIPFLatG4QHMhUHvzSKYVHQgpcImU9qb4aDRmII4IsaFwPjEYK

=======================================================================================

bat echo 输出内容为不同的颜色

先看代码:

复制代码
@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 echo say the name of the colors, don't read
:start
rem cls
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"

echo.
echo aaaaaa

pause

goto :eof

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

使用注意事项:

1.你的代码放在 :start 和第一行 goto :eof之间

2.换行使用 echo.

3.最好使用双引号把内容括起来,不然遇到空格就会报拒绝访问的错误,内容如果包含特殊字符(例如:"a:!aa" 等等,一般是win不允许我们用来命名文件的符号),也可能导致报错,或者字符不显示,所以最好不要使用特殊字符,这个会在当前目录生成一个临时文件,以你的内容为命名,如果包含了那些字符,就会产生那些文件,最好不删除,需要自己手动删除。

4.关于颜色,有背景色和字体色,第一个数字代表背景色,第二个数字代表字体色,至于颜色值,在 cmd 中输入 color /? 查看

 

出处:https://www.cnblogs.com/ibingshan/p/10906622.html

=======================================================================================

今天从网上看到一个强制删除的脚本,其中里面有使用到指定颜色显示的,感觉还不错,我自己也修改了一下,代码参考如下:

@echo off
call :colortheword "将着色的字符" 37 "使用默认颜色显示"
goto :eof

rem 在参数中<>为必选项,[]为可选项,调用参数:<str1=将着色的字符> [str2=颜色设置] [str3=正常显示字符] 
:colortheword <str1=将着色的字符> [str2=颜色设置] [str3=正常显示字符] 
set "objFile=%~1"
set "objColor=07"&if not "%~2."=="." set "objColor=%~2"
set "objMsg= "&if not "%~3."=="." set "objMsg=%~3"
for /F %%a in ('"prompt $h & for %%b in (1) do rem"')do set /p="%%a%objMsg%"<nul>"%objFile%"
findstr /a:%objColor% .* "%objFile%" nul
del /q "%objFile%" >nul 2>nul
goto :eof

 

=======================================================================================

CMD/bat里使用彩色(示例+函数)

本帖最后由 hnfeng 于 2024-6-21 21:45 编辑
批处理使用彩色字符和背景,我知道的有三种:
1、使用第三方工具,例如 syba.exe,  echoX.exe,  CoColor.exe 等等,目前知道的只支持16色
2、使用 findstr,优点是可以在win10(不含)之前的系统中使用,缺点是可能有点字符会有问题。也是只支持16色
3、使用转义,优点是支持16色、256色、真彩色,缺点是只支持win10(含)以后的系统

这几天研究了下转义方式,在cmd/bat里面使用彩色,写了个函数方便自己使用,分享出来
批处理里面的演示部分,有的并没有使用函数,而是直接输出,你比较一下就知道怎么用,很简单的

@echo off
for /f "delims=#" %%i in ('prompt #$E#^&echo on^&for %%a in ^(1^) do rem') do set EscK=%%i
setlocal EnableDelayedExpansion&cd /d "%~dp0"&title %~nx0  [hnfeng]&color 07
mode con lines=40 cols=182
for /l %%i in (1,1,40) do set bar=!bar!=

:: 3/4位(8色/16色):
echo 3/4位(8色/16色):
echo %bar%
for /l %%i in (30,1,37) do set /p "str=%EscK%[%%i;40m %%i %EscK%[m"<nul
for /l %%i in (30,1,37) do set /p "str=%EscK%[1;%%i;40m %%i %EscK%[m"<nul
for /l %%i in (90,1,97) do set /p "str=%EscK%[%%i;40m %%i %EscK%[m"<nul
echo;
for /l %%i in (30,1,37) do set /p "str=%EscK%[7;%%i;40m %%i %EscK%[m"<nul
for /l %%i in (30,1,37) do set /p "str=%EscK%[7;1;%%i;40m %%i %EscK%[m"<nul
for /l %%i in (90,1,97) do set /p "str=%EscK%[7;%%i;40m %%i %EscK%[m"<nul
echo;&echo;

:: 8位(256色):
echo 8位(256色):
echo %bar%
set Invert=
call :DoDisp %Invert%
echo;&echo;
::(8位)256色背景(使用反显的方法)
set Invert=i
call :DoDisp %Invert%
echo;&echo;&echo;

:: 24位(RGB真彩色):
echo 24位(RGB真彩色):
echo %bar%
for /L %%r in (64,2,255) do (call :EchoC " " "38;2;%%r;0;0" x 40 i
  ping -n 1 127.1>nul)
echo;
for /L %%g in (64,2,255) do (call :EchoC " " "38;2;0;%%g;0" x 40 i
  ping -n 1 127.1>nul)
echo;
for /L %%b in (64,2,255) do (call :EchoC " " "38;2;0;0;%%b" x 40 i
  ping -n 1 127.1>nul)
echo;&echo;

for /L %%g in (0,8,255) do (
  set /p "str=%EscK%[30;48;2;255;%%g;0m %EscK%[m"<nul
  ping -n 1 127.1>nul)
for /L %%r in (255,-8,0) do (
  set /p "str=%EscK%[30;48;2;%%r;255;0m %EscK%[m"<nul
  ping -n 1 127.1>nul)
for /L %%b in (0,8,255) do (
  set /p "str=%EscK%[30;48;2;0;255;%%bm %EscK%[m"<nul
  ping -n 1 127.1>nul)
for /L %%g in (255,-8,0) do (
  set /p "str=%EscK%[30;48;2;0;%%g;255m %EscK%[m"<nul
  ping -n 1 127.1>nul)

echo;&echo;&echo;
endlocal
pause
exit /b



:DoDisp %Invert%
for /l %%i in (0,1,15) do (
    set /a text=1000+%%i
    rem call :EchoC " !text:~-3! " "38;5;%%i" x 40 %1
    call :EchoC " !text:~-3! " "38;5;%%i" x "48;5;0" %1
)
echo;
set /a N=36
set /a L=0
for /l %%i in (16,1,231) do (
    set /a text=1000+%%i
    call :EchoC " !text:~-3! " "38;5;%%i" x 40 %1
    rem call :EchoC " !text:~-3! " "38;5;%%i" x "48;5;0" %1
    set /a L+=1
    if !L! GEQ %N% (
      set /a L=0
      echo;
    )
)
for /l %%i in (232,1,255) do (
    set /a text=1000+%%i
    call :EchoC " !text:~-3! " "38;5;%%i" x 40 %1
    rem call :EchoC " !text:~-3! " "38;5;%%i" x "48;5;0" %1
)
goto :EOF



::===============================================================
:EchoC
::EchoC  <text> <ForegroundColor> [Return] [BackgroundColor] [Invert]
:: <text> ---- 字符
::  <ForegroundColor> 前景色
::    3/4位色-前景色      31                  范围 30-37
::    3/4位色-高亮前景色  "1;36"              范围 30-37
::                    或  91                  范围 90-97
::    8位(256色)          "38;5;100"          范围 0-255
::    24位(RGB色)         "38;2;255;128;128"  RGB 范围 0-255
::  <ForegroundColor> 背景色
::    3/4位色-背景色      41                  范围 40-47
::    3/4位色-高亮背景色  103                 范围 100-107
::    8位(256色)          "48;5;100"          范围 0-255
::    24位(RGB色)         "48;2;255;128;128"  RGB 范围 0-255
:: [Invert] 是否反显:I-反显,其他-不反显
:: [Return] 显示字符后是否换行:R-换行,其他-不换行
::
:: 例子:call :EchoC "Text 文字" 31
::       call :EchoC "Text 文字" "1;36" x 40 i
::       call :EchoC "Text 文字" "38;5;99" r
::       call :EchoC "Text 文字" "38;2;255;255;16" R "48;2;0;94;94"
::       call :EchoC "Text 文字" "38;2;255;128;64" R 46
::CMD中可使用下列命令显示全部256色("^["是"Ctrl+["):
::for /l %i in (0,1,255) do @echo ^[[38;5;%im_%i_%i_%i_%i_%i_%i_%i_%i_%i_%i^[[m
::for /l %i in (0,1,255) do @echo ^[[7;38;5;%im_%i_%i_%i_%i_%i_%i_%i_%i_%i_%i^[[m
setlocal EnableDelayedExpansion
if /i %5_ EQU I_ (
  set /p "str=%EscK%[7;%~2;%~4m%~1%EscK%[m"<nul
) else (
  if %4_ EQU _ (set /p "str=%EscK%[%~2;40m%~1%EscK%[m"<nul
  ) else (set /p "str=%EscK%[%~2;%~4m%~1%EscK%[m"<nul)
)
if /i %3_ EQU R_ echo;
endlocal&goto :EOF




::<ForegroundColor> 前景色
::[BackgroundColor] 背景色
::  1. 3/4位色(8/16色):
::    前景色 30-黑,31-红,32-绿,33-黄,34-蓝,35-品红,36-青,37-白
::      16色就是加上高亮的8色
::          开始高亮前景  [1;
::          关闭高亮前景  [22;
::       90-灰,91-亮红,92-亮绿,93-亮黄,94-亮蓝,95-亮品红,96-亮青,97-亮白
::    背景色     40-黑,41-红,42-绿,43-黄,44-蓝,45-品红,46-青,47-白
::    高亮背景色 100-灰,101-亮红,102-亮绿,103-亮黄,104-亮蓝,105-亮品红,106-亮青,107-亮白
::  2. 8位色(256色):
::    256色前景色及引导 [38;5;n
::    256色背景色及引导 [48;5;n
::      n为颜色值:
::        0-黑,1-红,  2-绿,  3-黄,  4-蓝,  5-品红,  6-青,  7-白
::        8-灰, 9-亮红,10-亮绿,11-亮黄,12-亮蓝,13-亮品红,14-亮青,15-亮白
::        16 至 231 :多种颜色
::        232 至 255:灰度,从较黑到较白(0最黑,15最白)
::  3. 24位色(RGB):
::    24位色前景色及引导 [38;2;R;G;B
::    24位色背景色及引导 [48;2;R;G;B
::      0 <= R,G,B <=255
::  4. 反显:
::    开启  [7;
::    关闭  [27;
::  5. 下划线:
::    开启  [4;
::    关闭  [24;
::
:: 下面内容有部分试验不出来
::  恢复默认:[0m/[m ;粗体(高亮?):[1m ;亮度减半开关:[2m/[22m ;下划线开关:[4m/[24m ;闪烁开关:[5m/[25m
::  反显开关:[7m/[27m ;隐藏(前景背景色一样)开关:[8m/[28m ;划掉开关:[9m/[29m;上划线开关:[53m/[55m
::  30-37:3/4位色前景色; 38;5:设置前景色(8位);38;2:设置前景色(24位)
::  40-47:3/4位色背景色; 48;5:设置背景色(8位);48;2:设置背景色(24位)

---------------------------------------------------------------------------------------

再给个例子

@echo off
for /f "delims=#" %%i in ('prompt #$E#^&echo on^&for %%a in ^(1^) do rem') do set EscK=%%i
setlocal EnableDelayedExpansion&cd /d "%~dp0"&title %~nx0&color 07
for /l %%v in (1,1,50) do (set bar=!bar!=)
echo;&echo;&call :EchoC "%Bar%" "38;5;33" r&echo;
For /f "tokens=2* delims==" %%s in ('wmic OS get Caption /value ^| findstr /i "Caption"') do set OS=%%s
call :EchoC "当前系统是:" "38;5;37"
call :EchoC "%OS%" "38;5;120" r
echo;&call :EchoC "%Bar%" "38;5;33" r&echo;&echo;

for /f "tokens=2 delims==" %%i in ('wmic path win32_operatingsystem get LocalDateTime /value ^| findstr "="') do (set "strDate=%%i")
echo;&echo 使用ECHO的方法
echo %EscK%[38;5;208m现在时间是:%EscK%[38;5;196m%strDate:~0,4%%EscK%[38;5;135m年%EscK%[38;5;202m%strDate:~4,2%%EscK%[38;5;135m月%EscK%[38;5;208m%strDate:~6,2%%EscK%[38;5;135m日%EscK%[38;5;214m%strDate:~8,2%%EscK%[38;5;135m点%EscK%[38;5;220m%strDate:~10,2%%EscK%[38;5;135m分%EscK%[38;5;226m%strDate:~12,2%%EscK%[38;5;135m秒%EscK%[m
echo;&echo 使用函数的方法
call :EchoC "现在时间是:" "38;5;208"
call :EchoC "%strDate:~0,4%" "38;5;196"
call :EchoC "年" "38;5;135"
call :EchoC "%strDate:~4,2%"  "38;5;202"
call :EchoC "月" "38;5;135"
call :EchoC "%strDate:~6,2%"  "38;5;208"
call :EchoC "日" "38;5;135"
call :EchoC "%strDate:~8,2%" "38;5;214"
call :EchoC "点" "38;5;135"
call :EchoC "%strDate:~10,2%" "38;5;220"
call :EchoC "分" "38;5;135"
call :EchoC "%strDate:~12,2%" "38;5;226"
call :EchoC "秒" "38;5;135" R

echo;&echo;
pause
exit /b


::===============================================================
:EchoC
setlocal EnableDelayedExpansion
if /i %5_ EQU I_ (set /p "str=%EscK%[7;%~2;%~4m%~1%EscK%[m"<nul
) else (if %4_ EQU _ (set /p "str=%EscK%[%~2;40m%~1%EscK%[m"<nul
  ) else (set /p "str=%EscK%[%~2;%~4m%~1%EscK%[m"<nul))
if /i %3_ EQU R_ echo;
endlocal&goto :EOF

image

---------------------------------------------------------------------------------------

这是逼着我用WIN10啊!
WIN7系统不支持转义符,但我有第三方工具echox,能实现简单的彩色字符。

---------------------------------------------------------------------------------------

echox 确实也不错。
syba.exe 是最好的一类,可以指定坐标
echox.exe 是第二类,可以保留光标不换行
Cocoloer.exe 是第三类,总是换行光标下移

 

出处:https://www.bathome.net/thread-69258-1-1.html

=======================================================================================

echoX.exe 用于批处理显示彩色字符

详情介绍

echoX.exe用于批处理显示彩色字符,更好看的界面,为用户提供更好的界面等。
EchoX 2.7 - (C) 2004-2010 by Bill Stewart (bstewart@iname.com)

Usage: echox [-c color | [-f fg] [-b bg]] [-n] [-w width [-r | -e]] message
   or: echox -l

-c       Specifies a color (01-FE).
-f, -b   Specifies the foreground and background colors (0 through F).
-n       Do not skip to the next line.
-w       Specifies the message should be 'width' characters wide.
-r       Right-aligns the message with respect to the specified width.
-e       Centers the message with respect to the specified width.
message  The text to be displayed.
-l       Lists available colors in a table.

When specifying colors, the foreground color cannot match the background color.
If you specify -c, you cannot specify -f or -b (and vice versa).

The message can contain the following escape sequences: ~n (return), ~r (line
feed), and ~t (tab). To display a literal '~' if it's followed by n, r, or t,
use '~~'. Escape sequences are ignored if -w is specified.

EchoX 2.7 - (C) 2004-2010 by Bill Stewart (bstewart@iname.com)

First digit = background, second digit = foreground

     01  02  03  04  05  06  07  08  09  0A  0B  0C  0D  0E  0F
 10      12  13  14  15  16  17  18  19  1A  1B  1C  1D  1E  1F
 20  21      23  24  25  26  27  28  29  2A  2B  2C  2D  2E  2F
 30  31  32      34  35  36  37  38  39  3A  3B  3C  3D  3E  3F
 40  41  42  43      45  46  47  48  49  4A  4B  4C  4D  4E  4F
 50  51  52  53  54      56  57  58  59  5A  5B  5C  5D  5E  5F
 60  61  62  63  64  65      67  68  69  6A  6B  6C  6D  6E  6F
 70  71  72  73  74  75  76      78  79  7A  7B  7C  7D  7E  7F
 80  81  82  83  84  85  86  87      89  8A  8B  8C  8D  8E  8F
 90  91  92  93  94  95  96  97  98      9A  9B  9C  9D  9E  9F
 A0  A1  A2  A3  A4  A5  A6  A7  A8  A9      AB  AC  AD  AE  AF
 B0  B1  B2  B3  B4  B5  B6  B7  B8  B9  BA      BC  BD  BE  BF
 C0  C1  C2  C3  C4  C5  C6  C7  C8  C9  CA  CB      CD  CE  CF
 D0  D1  D2  D3  D4  D5  D6  D7  D8  D9  DA  DB  DC      DE  DF
 E0  E1  E2  E3  E4  E5  E6  E7  E8  E9  EA  EB  EC  ED      EF
 F0  F1  F2  F3  F4  F5  F6  F7  F8  F9  FA  FB  FC  FD  FE    

实例:
echox.exe -c 0a -n "正在备份--> "

image

普通下载地址

 

 

出处:https://www.jb51.net/softs/430220.html

=======================================================================================

posted on 2015-06-14 20:48  jack_Meng  阅读(9549)  评论(1)    收藏  举报

导航