How to change the text color of comments line in a batch file

前端 未结 3 1483
感情败类
感情败类 2020-12-17 07:22

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         


        
3条回答
  •  误落风尘
    2020-12-17 08:10

    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 ;-).

提交回复
热议问题