Color echo in batch files

It’s probably easier if you call a custom C++ program.

Although it’s not really necessary to do something like this, it’s possible.

There’s a pre-made C++ script compiled into something Windows can run at http://www.codeproject.com/Articles/17033/Add-Colors-to-Batch-Files

The site explains mostly everything you need to know, but I’ll give some help an examples here.

The color codes you can use are

  • 0 = black 8 = gray
  • 1 = navy 9 = blue
  • 2 = green A = lime
  • 3 = teal B = aqua
  • 4 = maroon C = red
  • 5 = purple D = fuchsia
  • 6 = olive E = yellow
  • 7 = silver F = white

I’m guessing what you want to do is color text, return color to normal and then make a new line. To do this, you would use the following…

cecho {0C}Hello world!{#}{\n}

When you run the above, “Hello world!” will appear in Red.

The first digit is the background, the second one is the foreground (actual text color).

Another example would be yellow on black:

cecho {0E}Yellow is a nice color.{#}{\n}

EDIT:

If you’re trying to color everything, use the color command that comes supplied with Windows.

color 0c

(Green on black)

EDIT:

If you’re looking to color a single line using only batch (no other resources), 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"
)
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

Credits to Tutankhamen for this one.

Original answer (From Tutankhamen) at https://stackoverflow.com/a/23072489/3931279

Leave a Comment