We use batch files for... well, pretty much everything. From simple files that simple optimize modified graphics, to the tendril-like files that build our software. For some time now, I've been using cecho.exe from a CodeProject article so that I highlight errors and successes. And this has worked fine - as long as I was running the scripts in a console window.

However, when running batch files through Visual Studio any output from cecho.exe simply wasn't displayed. That was something I ignored. However, over the last couple of days I've been finally setting up a CI server and have been testing both Jenkins and TeamCity and I had the exact same behaviour of blank lines when running builds in both of these tools - that I can't ignore.

I had a cursory glance through the C++ code from the original article and while it looks fine, I make no pretence of being a C++ developer. Given that the past two weeks I've been working with PHP and F#, I not in a hurry to study a 3rd extra language!

I had observed that my own console tools which used coloured output appeared perfectly well in Visual Studio, Jenkins and TeamCity so I decided I would replicate the cecho.exe tool using C#.

Using the tool

As I'm not in a hurry to change all the batch files calling cecho I've kept the exact same syntax (and for the time being the same annoying behaviour regarding having to manually reset colours and include line breaks).

  • {XX}: colours coded as two hexadecimal digits. E.g., {0A} light green
  • {color}: colour information as understandable text. E.g., {light red on black}
  • {\n}: New line character
  • {\t}: Tab character
  • {\u0000}: Unicode character code
  • {{: escape character {
  • {#}: restore foreground colour
  • {##}: restore foreground and background colour

Colours are defined as

  • 0: Black (black)
  • 1: Dark Blue (navy, dark blue)
  • 2: Dark Green (green, dark green)
  • 3: Dark Cyan (teal, dark cyan)
  • 4: Dark Red (maroon, dark red)
  • 5: Dark Magenta (purple, dark magenta)
  • 6: Dark Yellow (olive, brown, dark yellow)
  • 7: Gray (silver, light gray, light grey)
  • 8: Dark Gray (gray, grey, dark gray, dark grey)
  • 9: Blue (blue, light blue)
  • A: Green (lime, light green)
  • B: Cyan (aqua, light cyan)
  • C: Red (red, light red)
  • D: Magenta (fuschia, magenta, light magenta)
  • E: Yellow (yellow)
  • F: White (white)

The names in brackets are alternatives you can use for understandable text.

Note

For backwards compatibility, this program behaves the same way as the original cecho - lines are not terminated with a carriage return and the colours are not reset. Therefore you should ensure you include {#} or {##} and {\n} at the end of your statements.

Or of course, just modify the source to do this automatically if you don't need compatibility.

Samples

This first example uses the shorthand notation to change ERROR: into red.

bat
cecho {0c}ERROR:{#} Signing failed for program1.exe, retrying{\n}
Basic example using hex codes
Basic example using hex codes

This example uses named colours instead of hex codes.

bat
cecho This {yellow on teal}word{##} is yellow on a teal background{\n}
Basic example using colour names
Basic example using colour names

This final example prints out an extended character.

bat
cecho {\u2593}
Printing extended characters
Printing extended characters

Getting the source

The source code this tool is available on our GitHub page.

Update History

  • 2015-04-09 - First published
  • 2020-11-21 - Updated formatting

Like what you're reading? Perhaps you like to buy us a coffee?

Donate via Buy Me a Coffee

Donate via PayPal


Comments