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
Click **here**
A JavaScript REPL for Android Devices
A Review of Kevin Mitnick's Book Ghost in the Wires
Play MP3 Files with Python on Windows
Extending SpiderMonkey JavaScript on Windows
Twimmando: A Command-line Twitter Client
A Simple Parser for a Small Command Line Interface
Screen Capture from Multiple Monitors in Java
Shrouding CSharp and Java Source Code with AWK
An Interview with the Creator of the BDS C Compiler
PHP, Transparent GIF's, and Web Tracking
Why Some Web Sites will go Dark on Jan 18th
Book Review : Paull Allen - Idea Man
A 90's Experiment in Online Systems - The U.S. West CommunityLink Service