Originally published on: Sat, 20 Mar 2010 23:37:57 +0000
Please note! If you're having difficulties compiling the C source code presented below, please see my post: Compiling C from the Command Line with Pelles C
Changing the text color in a Windows batch file can be done using the built-in color command. This command changes the color for the entire screen.
I wrote a utility called chgcolor that will set the current output color scheme so that only the output text that is written to the console window after using the utility is seen in the new color. When coupled with my echoj utility ( see Yet Another Enhanced Echo Command ), one can devise batch files that color individual sections of text.
The chgcolor utility accepts a single parameter that represents both the background color and the foreground color. Each is specified as a single hexadecimal digit from the table below:
If you invoke the command...
chgcolor 46
...you should then see all output as yellow text on a red background. The background color is specified as the first digit. If that digit is omitted, the background color will be set to black.
chgcolor 7
The above command sets the output color to white on black.
When you couple chgcolor with echoj, you can color specific words in your output text in a batch file. The echo command itself always writes a complete line of text, so echoj is used to output small fragments of text after changing the colors.
trycolor.bat
If you run the above batch file, you'll see a single line of output that asks "How are you today?. The word "you" is emphasized in bright white text on a red background.
The C source code for chgcolor is as follows:
chgcolor.c
If you decide to tinker with the source code, you may also wish to read my post Setting Windows Console Text Colors in C.
The source code and executable file for colordemo can be downloaded from a single archive at: http://www.mailsend-online.com/wp/chgcolor.zip
Unless otherwise noted, all code and text entries are Copyright ©2010 by James K. Lawless
Views expressed in this blog are those of the author and do not necessary reflect those of the author's employer. Views expressed in the comments are those of the responding individual.

Save to StumbleUpon
Digg it
Save to Reddit
Share on Facebook
Share on Twitter
More bookmarks
Compiling C from the Command Line with Pelles C
A Forthcoming Marvel Movie Villain
Uninstalling Problematic Windows Software
A JavaScript REPL for Android Devices
Invoking the Default Windows Screen-Saver
Java in a Windows EXE with launch4j
COM Scripting in C by way of JavaScript
Switching a Console Window to Full Screen or Windowed Mode
TAP : A Command Processor Library
Extending SpiderMonkey JavaScript on Windows
Learning Z-80 Assembly Language on the TRS-80
Uninstalling Problematic Windows Software
My First C64 Interrupt Program
Playing with OS/X Text to Speech
The Villain at the end of Marvel's Avengers Move is...
Processing GMail items with Ruby
Happy 30th Birthday, Commodore 64
MailWrench CSharp Command Line Mailer for Windows is now Free Software
Rainy Day Fun with the HTML DOM
Building a World War II Foxhole Radio
Prototerp – Unleashing a JavaScript REPL via the Mobile Browser