我知道color bf命令设置了整个命令行窗口的颜色,但我想打印不同颜色的单行。


当前回答

我看这个是因为我想在Win7批处理文件中引入一些简单的文本颜色。这是我想到的。谢谢你的帮助。

@echo off
cls && color 08

rem .... the following line creates a [DEL] [ASCII 8] [Backspace] character to use later
rem .... All this to remove [:]
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (set "DEL=%%a")

echo.

<nul set /p="("
call :PainText 09 "BLUE is cold"    && <nul set /p=")  ("
call :PainText 02 "GREEN is earth"  && <nul set /p=")  ("
call :PainText F0 "BLACK is night"  && <nul set /p=")"
echo.
<nul set /p="("
call :PainText 04 "RED is blood"    && <nul set /p=")  ("
call :PainText 0e "YELLOW is pee"   && <nul set /p=")  ("
call :PainText 0F "WHITE all colors"&& <nul set /p=")"

goto :end

:PainText
<nul set /p "=%DEL%" > "%~2"
findstr /v /a:%1 /R "+" "%~2" nul
del "%~2" > nul
goto :eof

:end
echo.
pause

其他回答

在powershell中为日志语句设置颜色并不是什么大问题。 你可以使用-ForegroundColor参数。

写一个确认消息。

Write-Host "Process executed Successfully...." -ForegroundColor Magenta

写入错误消息。

Write-Host "Sorry an unexpected error occurred.." -ForegroundColor Red

写一个进度消息。

Write-Host "Working under pocess..." -ForegroundColor Green

您可以使用ANSICON在旧版本的Windows中启用ANSI终端代码。我在Windows XP和Windows 7中使用的是32位和64位版本。

这不是一个很好的答案,但如果你知道目标工作站有Powershell,你可以这样做(假设BAT / CMD脚本):

CALL:ECHORED "Print me in red!"

:ECHORED
%Windir%\System32\WindowsPowerShell\v1.0\Powershell.exe write-host -foregroundcolor Red %1
goto:eof

编辑:(现在更简单了!)

这是一个老答案,但我想我要澄清和简化一点

PowerShell现在包含在从7开始的所有版本的Windows中。因此,这个答案的语法可以缩短为一种更简单的形式:

路径不需要指定,因为它应该已经在环境变量中。 明确的命令可以缩写。例如,你可以: 使用-fore代替-foregroundcolor 使用-back代替-backgroundcolor 这个命令也可以在“内联”中代替echo (而不是像上面那样创建一个单独的批处理文件)。


例子:

powershell write-host -fore Cyan This is Cyan text
powershell write-host -back Red This is Red background

更多信息:

完整的颜色列表和更多信息可在 - PowerShell Write-Host文档

下面的代码由两部分组成。如果方便的话,在这个。cmd文件中还有一个。txt格式的文件,在“double”行(====)下面。

::adonios77
::This is a .cmd file
@ECHO OFF
TITLE Colored Command Prompt echoes HELP
mode con: cols=55 lines=47
CLS
COLOR 0f 
echo [93m
ECHO This is just help, as optical example,
ECHO when make or modify colorful command prompt echoes.
ECHO.
ECHO More info in Source:
ECHO [4m[94mhttps://stackoverflow.com/questions/2048509/how-to-echo-with-different-colors-in-the-windows-command-line[0m

ECHO.
ECHO [0mESC[0m "Text" Default colours Text[0m
ECHO [7mESC[7m "Text" Inversed Back-Fore colors[0m 
ECHO [101mESC[101m "Text" in Red Background[0m
ECHO [91mESC[91m "Text" in Red Foreground)[0m

echo.
echo To make an ESC special character, (ASCII Escape code)
echo open or edit a .txt or .bat or .cmd file,
echo (hold)L-Alt and (type)027 in NumPad)
echo Or, in Command Prompt, (can't copy/paste special char.)
echo just press Ctrl+[ 
echo  (it should look like: "echo ^[[33m'Text'^[[0m")
echo. 
echo STYLES
echo [0mESC[0m Reset[0m
echo [1mESC[1m Bold [90m*This is not work for me[0m
echo [4mESC[4m Underline[0m
echo [7mESC[7m[0m Inverse
echo. 
echo COLORS#  Foreground-Background (color /? HEX) && echo.
echo           [90mDark[0m     /    [100mLight[0m
echo        Fore-Back   /  Fore-Back
echo Black  *   [100m[30m30[0m-[4m[40m40  [0m   (0) / (8) [90m90[0m-[100m100 [0m
echo Red        [31m31[0m-[41m41  [0m   (4) / (C) [91m91[0m-[101m101 [0m
echo Green      [32m32[0m-[42m42  [0m   (2) / (A) [92m92[0m-[102m102 [0m
echo Yellow         [33m33[0m-[90m[43m43  [0m   (6) / (E) [93m93[0m-[90m[103m103 [0m
echo Blue       [34m34[0m-[44m44  [0m   (1) / (9) [94m94[0m-[104m104 [0m
echo Magenta    [35m35[0m-[45m45  [0m   (5) / (D) [95m95[0m-[105m105 [0m
echo Cyan       [36m36[0m-[46m46  [0m   (3) / (B) [96m96[0m-[106m106 [0m
echo White  *   [37m37[0m-[47m47  [0m   (7) / (F) [97m97[0m-[7;97m107 [0m
echo. 
echo Note: use ESC[0m at the end of (every) line.
echo. 
echo COMBINATIONS
echo [7;91mESC[7;91m inverse red foreground color ESC[0m[0m
echo. 

ECHO. && PAUSE
exit

============================================================
:: This is a .txt file.
 This is just help, as optical example,
 when make or modify colorful command prompt echoes.

 More info in Source:
https://stackoverflow.com/questions/2048509/how-to-echo-with-different-colors-in-the-windows-command-line

To make an ESC special character, (),
open or edit a .txt or .bat or .cmd file,
(hold)L-Alt and (type)027 in NumPad)

STYLES
[0m Reset
[1m Bold
[4m Underline
[7m Inverse

COLORS#  (Foreground-Background)
            Dark        /     Light
        Fore-Back       /   Fore-Back
Black       30-40   (0) / (8)   90-100
Red         31-41   (4) / (C)   91-101
Green       32-42   (2) / (A)   92-102
Yellow      33-43   (6) / (E)   93-103
Blue        34-44   (1) / (9)   94-104
Magenta     35-45   (5) / (D)   95-105
Cyan        36-46   (3) / (B)   96-106
White       37-47   (7) / (F)   97-107

COMBINATIONS
ESC[7;31m inverse red foreground color 0m

Note: use ESC[0m at the end of (every) line.

examples:
@ECHO OFF
ECHO          Default Text
ECHO [7m"Text" Inversed Back-Fore colors (7m)[0m 
ECHO [101m"Text" in Red Background (101m)[0m
ECHO [91m"Text" in Red Foreground (91m)[0m

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

另外,我发现用这种方式可以改变命令提示符的样子,暂时的或永久的。 下面的TEXT代码是一个例子:

This is a .txt file. Antony's examples: prompt $Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$S $T$_ $P\$_$G gives something like that: ==================== 19:53:02,73 C:\Windows\system32\ > For All Users & Permanent: (if there is space between characters, must double quoted [""]) SETX PROMPT /M $Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$S$S$T$_$_$S$P\$_$G$S gives something like that: ==================== 9:01:23,17 C:\Windows\system32\ > NOTE: Variables created or modified by SETX will be available at the next logon session.

现在让我们给上面的例子配上颜色。结果如上图所示。

彩色提示示例:

仅适用于当前用户:

prompt $E[91m$E[40m$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$S $T$E[93m$_ $P\$_$G$E[0m

or

对于所有用户和永久用户:

SETX PROMPT /M $E[91m$E[40m$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$S$S$T$E[93m$_$_$S$P\$_$G$S$E[0m

Windows 10 - TH2及以上版本:

(a.k.a Version 1511, build 10586, release 2015-11-10)

在命令提示符:

echo ^[[32m HI ^[[0m

使用实际按键:echo Ctrl+[[32m HI Ctrl+[[0mEnter .

您应该在它下面看到一个绿色的“HI”。

代码可以在这里找到:

https://en.wikipedia.org/wiki/ANSI_escape_code#Colors

记事本:

要保存到记事本,你可以用Alt+027和numpad键入ESC,然后[32m部分。当我使用笔记本电脑时,另一个技巧是将上面的行重定向到一个文件中,然后剪切和粘贴:

echo echo ^[[32m HI ^[[0m >> batch_file.cmd