Different Colors in Batch File

by RayOkay in Circuits > Computers

33 Views, 0 Favorites, 0 Comments

Different Colors in Batch File

giraffe.jpg

introduction

The color command in batch files changes the color of the terminal. This is very limited however as it does it for the entire screen, instead of one line. I have made a function that allows you to change the color of each individual line. Credit to Prof. Pickle for the idea.

Supplies

A Windows Computer (preferably 7+)

A text editor (any will work)

Basic knowledge of computers (how to navigate folders, etc.)

How to Use

Copy this code and paste it to EXACTLY this file name: fecho.bat

@echo off

REM named "fecho" bcs it stands for formatted echo

if "%~1"=="/?" goto :showhelp
if "%~1"=="" goto :showhelp
if "%~2"=="" goto :showhelp

set "text=%~1"
set "color=%~2"

powershell -Command "Write-Host \"%text%\" -ForegroundColor %color%"
goto :eof

:showhelp
echo Prints text with a custom color.
echo:
echo FECHO [text] [color]
echo:
echo text The text to print
echo color Specifies color of text
echo:
echo Use the chart below for possible COLOR attributes (case-sensitive):
echo:
echo Black
echo DarkBlue
echo DarkGreen
echo DarkCyan
echo DarkRed
echo DarkMagenta
echo DarkYellow
echo Gray
echo DarkGray
echo Blue
echo Green
echo Cyan
echo Red
echo Magenta
echo Yellow
echo White

Now go to your root drive (usually C:\) and Windows, then System32. Copy that fecho.bat file into that folder and if it asks you if it needs admin permissions accept it. Open the cmd and type in:

fecho /?

This will give you the info on how to use it. For example:

fecho "Hello, World!" Yellow

returns this:

If you want to call it from a batch file you can use this:

call fecho *parameters*

To keep it portable (or if you don't want to do that yourself), you can use this installer here.

How It Works


part 1: the function


To understand how the function works, we need to step out of the picture and learn how the terminal really runs commands. Basically in the System32, whenever you type a command, it finds the .bat, .exe, or .com file corresponding to it and runs it. This is how we make this custom command. Now let's break down the source code:


@echo off

Standard batch file logic. This blocks extra useless info from clogging up the screen.

if "%~1"=="/?" goto :showhelp
if "%~1"=="" goto :showhelp
if "%~2"=="" goto :showhelp

The %1 is the first argument. Adding ~ removes any excess quotes around it. The "==" is the equals operator. It checks whether %~1 is "/?" (help argument) or empty space. If it is either one of those, it goes to the label "showhelp", which displays help information about the function and exits. The same goes for %~2.

set "text=%~1"
set "color=%~2"

This sets the first argument without excess quotes (%~1) to the variable text. It then sets the second argument without excess quotes (%~2) to the variable color.

powershell -Command "Write-Host \"%text%\" -ForegroundColor %color%"
goto :eof

This runs a powershell command. "powershell" means "run the powershell program" and "-Command" means that there will be a command argument. Write-Host means "print this to the console" and %text% is the text variable. The forward slashes (\) are escape characters, meaning that the quotes inside the quotes are a literal part of the string, not terminating it. -ForegroundColor is an argument that takes the color variable as the text color. Finally, goto :eof means "go to the end of the file", stopping the command.