Colored text in Batch script

Unsteady Journal of Jolty Notions

I ‘ve just written a litte batch script that connects the adb to my tablet via wlan. But the script does not always end with success since wlan is always a little shaky. So I needed an easy way to see if it did connect or not. Coloring is the first thing that comes to mind. Green, all ok; Red, smt went wrong. Well guess what, Batch / Dos does not allow colored text by default…

After googling a litte bit around I decided to write this following litte tool. It is in c#. I guess this is also my first c# programm actually. Or is it ? I can’t remember if I ever worked c# before, well whatever.

It takes an arbitrary amount of arguments, but at least 3. First is the foreground color, second is the background color, and from the third on it is the colored text. So if you want “something” in red, you just call:

col Red Black something

You want to emphasise it even more ? Just call:

col Black Red something some thing

I needed to name this just “col” instead of “color” since that is a DOS variable that sets the color of the whole console, maybe that would have also sufficed to show success or failure, but this way it was more time consuming 🙂 I just love to procrastrinate, don’t I ?

Well here is the code :

using System;

namespace Colors
{
    class MyColor
    {
        static void Main(string[] args)
        {
            if (args.Length < 3)
            {
                System.Console.WriteLine("\n    Colors by Burak\n");
                System.Console.WriteLine("Usage:");
                System.Console.WriteLine("color [ForegroundColor] [BackgroundColor] [text]");
                System.Console.WriteLine("\n All parameters needed ! \n\n Choose from: Black, DarkBlue, DarkGreen, DarkCyan, DarkRed, DarkMagenta, DarkYellow, Gray, DarkGray, Blue, Green, Cyan, Red, Magenta, Yellow, White \n\n");
                Environment.Exit(0);
            }

            Type type = typeof(ConsoleColor);

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

            for (int i = 2; i< args.Length-1 ; i++)
            {
                Console.Write(args[i]+" ");
            }
            Console.Write(args[args.Length-1]);
            Console.ResetColor();

        }
    }
}

Well, so far so good.. The reason I am actually writing this is that I learned that for simple C# code snipplets like this one, you do not need a big complier or an SDK or fancy tools. Just the Command Prompt is enough!

Within the .NET Framework is a nice little compiler called csc. I guess it stands for “C Sharp Compiler”. So if you put the above code in a text file and open a Command Prompt in that folder (I’ll go back to this later) you can rename the text file to a more accurate name with something like this:

ren Untitled.txt col.cs

Since this is a C-sharp file .cs is better. And to go ahead and compile it you can call:

c:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe col.cs

So, what is this ?
In every .NET Framework you install, also a couple of tools are included. One of those is csc.exe. Of course the .exe is optional while calling it and instead of the v4.0.30319 you can use any other version you have installed. If there are Commands in your source that are not supported, it will throw an error, in that case use a higher version number. You can use Autocompletion to <tab> through all the versions availabe to you, type everything up to the version number type a single v and then use <tab>.

This should output a col.exe that works in any .NET preinstalled computer which has at least one compatible version.

Back to the “open a Command Prompt in that folder” :
I have very often had the need to to something in the Prompt, while or after shifting around files with a Windows Explorer window. All the time I did think the only way to get quickly into that folder is to type in the name with a cd command. I just recently discovered that this is acutally not true, you can simply do a right click and open a Command Prompt in exactly the folder you are in at that moment. You just need to hold the <Shift> key ! Knowing this will save me some time in the future, so I wanted to keep it written somewhere.