As noted by dbenham's comment, this is discussed by StackOverflow's how to have multiple colors in a batch file?
Reviewing that question, it is about Windows 7. The answers there are all requiring extra software, or using some batch file techniques which look... a bit complex.
However, this question doesn't have an operating system tagged, and I'd like to add new information relevant to a newer operating system. Windows 10 has added support for ANSI Escape sequences.
The beauty of ANSI escape sequences is that using them is conceptually easy. You just need to use ANSI color escape sequences (like those documented at Rob van der Woude's page on ANSI Color) and include them in an ECHO statement.
Unfortunately, using these sequences does get to be a little bit harder due to handling of the 27th ASCII character.
Now, the trick to using ANSI Escape sequences is to send the 27th ASCII character to the screen. The reason this is tricky is because the keyboard key that is most commonly related to ASCII 27 is the Esc key. (This is a main reason why these ANSI Escape sequences are often called "escape" sequences.) And a lot of software has special support for the Esc key. This special support often gets in the way of easily just sending a plain ASCII character number 27.
A lot of times people have worked around this by storing the 27th character in a file, and just copy-and-paste the character as needed. Then, making a file with lots of ANSI sequences involves using the 27th ASCII character a bunch of times, which may be doable using copy-and-paste.
However, there is a better approach. Just get the ASCII character into a file once, and get it into an environment variable. This works well in Windows 10 (and, from what I've read, UNIX terminals). Then you can just refer to that environment variable.
I managed to make a batch file using ssed. The batch file, mentioned some more below, simply sets a variable named ASCII 27 to contain that character.
Furthermore, you can create environment variables. So, instead of something like:
echo %ASCII27%[31m Lava %ASCII27%[37m Cyan/Aqua/Teal text %ASCII27%[33m Sun
echo %ASCII27%[34m Water %ASCII27%[37m Sky %ASCII27%[33m Sun
You can use:
echo %ANSIREDFG% Lava %ANSICYANFG% Cyan/Aqua/Teal text %ANSIYELLOWFG% Sun
echo %ANSIBLUEFG% Water %ANSICYANFG% Sky %ANSIYELLOWFG% Sun
That might not look a whole lot shorter. The advantage here isn't necessarily making the file prettier, but so you can use friendlier names (which you decide upon) like
%ANSIBLUEFG% to use BLUE as the Foreground color, instead of needing to remember which color is related to which number. That may be a lot faster to type out and debug (if you don't have the color numbers memorized).
I created a ZIP file containing:
- OUT.ANS: a sample ANSI file to show this off, containing multiple color layouts per line
- ANSICOL.BAT: a batch file that was used to create that ANSI file, demonstrating usage of variable names instead of frequent numeric references to the color numbers,
- ASCII27.BAT: creates the ASCII27 variable containing the escape sequence
This ZIP file is at Windows 10 ANSI demonstration, version 1. The batch file was made to work well with Windows 10. Due to use of “
%^%” this might not work quite as wonderfully in some older operating systems (but would probably work as expected if replacing “%^%” with “% %”).
If you don't feel like messing with environment variables, you could try just copying and pasting the codes straight from the out.ans file (which may work marvelously well, if your text editor doesn't corrupt the ASCII 27 character).