tags:

views:

391

answers:

2

I have a batch file as follows:

myfile.bat
:: This is a sample batch file

@echo off
echo change directory to d: <---How to change color of only this line (comments lines)
CD d:\
...
A: 

There is no built-in way of doing this. I suggest you write yourself a little helper program which either changes the color attributes of text to come or writes some text with specific color attributes.

In C# this could look like the following:

using System;

class SetConsoleColor {
    static void Main(string[] args) {
        if (args.Length < 3) {
            Console.Error.WriteLine("Usage: SetConsoleColor [foreground] [background] [message]");
            return;
        }

        Console.ForegroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), args[0], true);
        Console.BackgroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), args[1], true);

        Console.WriteLine(args[2]);

        Console.ResetColor();
    }
}

Feel free to port to C or another language you like; this was just the fastest way for me after struggling with a 50-line C monster which still didn't work ;-).

Joey
A: 

This is source code for a program that does what you want: http://www.mailsend-online.com/blog/setting-text-color-in-a-batch-file.html

I am beginning to think that there is no longer a built-in way to do this without an additional program, or modifications to the user's system.

An aside - For my scenario, if modifications to the user's system was a requirement, I'd simply opt to use python, IronPython, or JScript.NET instead.

Merlyn Morgan-Graham