Set the Color for Console using Windows Batch


If you want to change the colour of the font or the background of the console, you can use the command color under the command shell (cmd.exe). The color takes a parameter that specifies colours for the foreground and the background. The complete usage manual is shown below.

color2 Set the Color for Console using Windows Batch batch script beginner programming languages tools / utilities

The colour combination is given below.

color Set the Color for Console using Windows Batch batch script beginner programming languages tools / utilities

However, this command only supports one color at a time i.e. the next color will overwrite its previous value. If multiple text colors are intended, we might have to use external programs. You can easily write a helpful utility that changes the color or you can use the findstr which searches the string in a file, and this command supports the configuration of colour for the strings that are found in the given file. The switch /a specifies the colour attribute with two hex digits (the same as command color)

@echo off
SETLOCAL EnableDelayedExpansion
for /F "tokens=1,2 delims=#" %%a in ("prompt & echo on & for %%b in (1) do 
rem"') do (
  set "DEL=%%a"
)

call :ColorText 0a "blue"
call :ColorText 0C "green"
call :ColorText 0b "red"
call :ColorText 19 "yellow"
call :ColorText 2F "black"
call :ColorText 4e "white"

goto :eof

:ColorText
<nul set="" p="" ".="%DEL%&quot;"> "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1

goto :eof

This allows multiple colours in the same console for different pieces of texts.

colortest Set the Color for Console using Windows Batch batch script beginner programming languages tools / utilities

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
369 words
Last Post: Print Big Characters using Windows Batch Programming
Next Post: An Idea: Collection of Linux-Utils using Windows Batch

The Permanent URL is: Set the Color for Console using Windows Batch

2 Comments

  1. Andre
    • DoctorLai

Leave a Reply