How to echo with different colors in the Windows command line

前端 未结 23 1200
野性不改
野性不改 2020-11-22 08:55

I know that the color bf command sets the colors of the whole command line window but I wanted to to print one single line in a different color.

相关标签:
23条回答
  • 2020-11-22 09:49

    I looked at this because I wanted to introduce some simple text colors to a Win7 Batch file. This is what I came up with. Thanks for your help.

    @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
    
    0 讨论(0)
  • 2020-11-22 09:49

    I'm adding an answer to address an issue noted in some comments above: that inline ansi color codes can misbehave when inside a FOR loop (actually, within any parenthesized block of code). The .bat code below demonstrates (1) the use of inline color codes, (2) the color failure that can occur when inline color codes are used in a FOR loop or within a parenthesized block of code, and (3) a solution to the problem. When the .bat code executes, tests 2 and 3 demonstrate the colorcode failure, and test 4 shows no failure because it implements the solution.

    [EDIT 2020-04-07: I found another solution that's presumably more efficient than calling a subroutine. Enclose the FINDSTR phrase in parentheses, as in the following line:

       echo success | (findstr /R success)
    

    ENDEDIT]

    Note: In my (limited) experience, the color code problem manifests only after input is piped to FINDSTR inside the block of code. That's how the following .bat reproduces the problem. It's possible the color code problem is more general than after piping to FINDSTR. If someone can explain the nature of the problem, and if there's a better way to solve it, I'd appreciate it.

    @goto :main
    :resetANSI
    EXIT /B
    rem  The resetANSI subroutine is used to fix the colorcode
    rem  bug, even though it appears to do nothing.
    
    :main
    @echo off
    setlocal EnableDelayedExpansion
    
    rem  Define some useful colorcode vars:
    for /F "delims=#" %%E in ('"prompt #$E# & for %%E in (1) do rem"') do set "ESCchar=%%E"
    set "green=%ESCchar%[92m"
    set "yellow=%ESCchar%[93m"
    set "magenta=%ESCchar%[95m"
    set "cyan=%ESCchar%[96m"
    set "white=%ESCchar%[97m"
    set "black=%ESCchar%[30m"
    
    echo %white%Test 1 is NOT in a FOR loop nor within parentheses, and color works right.
       echo %yellow%[Test 1] %green%This is Green, %magenta%this is Magenta, and %yellow%this is Yellow.
       echo %Next, the string 'success' will be piped to FINDSTR...
       echo success | findstr /R success
       echo %magenta%This is magenta and FINDSTR found and displayed 'success'.%yellow%
       echo %green%This is green.
    echo %cyan%Test 1 completed.
    
    echo %white%Test 2 is within parentheses, and color stops working after the pipe to FINDSTR.
    (  echo %yellow%[Test 2] %green%This is Green, %magenta%this is Magenta, and %yellow%this is Yellow.
       echo %Next, the string 'success' will be piped to FINDSTR...
       echo success | findstr /R success
       echo %magenta%This is supposed to be magenta and FINDSTR found and displayed 'success'.
       echo %green%This is supposed to be green.
    )
    echo %cyan%Test 2 completed.
    
    echo %white%Test 3 is within a FOR loop, and color stops working after the pipe to FINDSTR.
    for /L %%G in (3,1,3) do (
       echo %yellow%[Test %%G] %green%This is Green, %magenta%this is Magenta, and %yellow%this is Yellow.
       echo %Next, the string 'success' will be piped to FINDSTR...
       echo success | findstr /R success
       echo %magenta%This is supposed to be magenta and FINDSTR found and displayed 'success'.
       echo %green%This is supposed to be green.
    )
    echo %cyan%Test 3 completed.
    
    echo %white%Test 4 is in a FOR loop but color works right because subroutine :resetANSI is 
    echo called after the pipe to FINDSTR, before the next color code is used.
    for /L %%G in (4,1,4) do (
       echo %yellow%[Test %%G] %green%This is Green, %magenta%this is Magenta, and %yellow%this is Yellow.
       echo %Next, the string 'success' will be piped to FINDSTR...
       echo success | findstr /R success
       call :resetANSI
       echo %magenta%This is magenta and FINDSTR found and displayed 'success'.
       echo %green%This is green.
    )
    echo %cyan%Test 4 completed.%white%
    
    EXIT /B
    
    0 讨论(0)
  • 2020-11-22 09:49

    The following code consists of two parts. If it is convenient for you too, there is also a .txt format in this .cmd file, below the "double" line (====).

    ::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
    
    ============================================================
    

    Also, I figured out that with this way can be change the way the Command Prompt looks like, temporarily or permanently. The following TEXT code is an example of:

    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.
    

    Now let's give colors to the above examples. The result in the image above.

    COLORED PROMPT examples:

    For only the current User:

    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

    For All Users and Permanently:

    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
    
    0 讨论(0)
  • 2020-11-22 09:51

    You can just creates files with the name of the word to print, uses findstr which can print in color, and then erases the file. Try this example:

    @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"
    )
    
    call :ColorText 0a "green"
    call :ColorText 0C "red"
    call :ColorText 0b "cyan"
    echo(
    call :ColorText 19 "blue"
    call :ColorText 2F "white"
    call :ColorText 4e "yellow"
    
    goto :eof
    
    :ColorText
    echo off
    <nul set /p ".=%DEL%" > "%~2"
    findstr /v /a:%1 /R "^$" "%~2" nul
    del "%~2" > nul 2>&1
    goto :eof
    

    Run color /? to get a list of colors.

    0 讨论(0)
  • 2020-11-22 09:51

    I just converted from Win 7 Home to Win 10 Pro and wanted to replace the batch I call from other batches to echo info in color. Reviewing what is discussed above I use the following which will directly replace my previous batch. NOTE the addition of "~" to the message so that messages with spaces may be used. Instead of remembering codes I use letters for the colors I needed.

    If %2 contains spaces requires "..." %1 Strong Colors on black: R=Red G=GREEN Y=YELLOW W=WHITE

    ECHO OFF
    IF "%1"=="R" ECHO ^[91m%~2[0m
    IF "%1"=="G" ECHO ^[92m%~2[0m
    IF "%1"=="Y" ECHO ^[93m%~2[0m
    IF "%1"=="W" ECHO ^[97m%~2[0m
    
    0 讨论(0)
提交回复
热议问题