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.


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


About Jim ...