Jim Lawless' Blog


Setting Text Color in a Batch File

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


@echo off
chgcolor 07
echoj "How are "
chgcolor 4f
echoj "you"
chgcolor 07
echoj " today?"

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


// Change the output color for the Windows console
//
// License: MIT / X11
// Copyright (c) 2009 by James K. Lawless
// jimbo@radiks.net http://www.radiks.net/~jimbo
// http://www.mailsend-online.com
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

#include <windows.h>
#include <stdio.h>
#include <string.h>

void *getConsoleFunction(char *name);

BOOL (WINAPI *doSetConsoleTextAttribute)(HANDLE hConsoleOutput, WORD attr);


int main(int argc,char **argv) {
   HANDLE hCon;
   WORD color;
   
   if(argc<2) {
      fprintf(stderr,"chgcolor - Change console text color - by Jim Lawless\n\n");
      fprintf(stderr,"Syntax:\n\tchgcolor attribute\n\n");
      fprintf(stderr,"See:\nhttp://www.mailsend-online.com/blog?p=77\nfor usage instructions and C source code.\n");
      return 1;
   }
   sscanf(argv[1],"%x",&amp;color);
   
   hCon=GetStdHandle(STD_OUTPUT_HANDLE);
   
   doSetConsoleTextAttribute=getConsoleFunction("SetConsoleTextAttribute");

   if(doSetConsoleTextAttribute==NULL) {
      fprintf(stderr,"Sorry! chgcolor is incompatible with this version of Windows.");
      return 1;
   }
   (*doSetConsoleTextAttribute)(hCon,color);

   return 0;
}

void *getConsoleFunction(char *name) {
   static HMODULE kernel32=(HMODULE)0xffffffff;
   if(kernel32==0)
      return NULL;
   if(kernel32==(HMODULE)0xffffffff) {
      kernel32=LoadLibrary("kernel32.dll");
      if(kernel32==0)
         return 0;
   }
   return GetProcAddress(kernel32,name);
}

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.

stumbleupon Save to StumbleUpon
digg Digg it
reddit Save to Reddit
facebook Share on Facebook
twitter Share on Twitter
aolfav More bookmarks


Previous post: Pi Day Meets the HTML5 Canvas
Next post:Setting Windows Console Text Colors in C


About Jim ...


Click **here**
to try out MailWrench;
a command-line SMTP /
SMTPS (Google Gmail)
mailer for Windows.


Follow me on Twitter

http://twitter.com/lawlessGuy


Recent Posts

A JavaScript REPL for Android Devices

MailSend is Free

My Blog Engine

The October 10th Bug

A Review of Kevin Mitnick's Book Ghost in the Wires

Spellbound by Web Programming

Backlinks to my Blog Posts

Play MP3 Files with Python on Windows


Random Posts

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

E-mail cleansing

Twimmando No More

Backlinks to my Blog Posts

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


Full List of Posts

http://www.mailsend-online.com/bloglist.htm


Recent Posts from my Other Blog

Remembering Dr. San Guinary

Why Some Web Sites will go Dark on Jan 18th

SNL Superhero Skit

More Ruby Games

My Ruby Game Challenge Entry

Steal this Bookmarklet

Nerd Toys

Learn New Jargon, You Must

Spot the Wiebe

Tech Magazine Glory Days

Book Review : Paull Allen - Idea Man

A 90's Experiment in Online Systems - The U.S. West CommunityLink Service