How to Change CMD Text Color Using Batch
- Batch Script or File
- Change the Text Color of CMD Using Batch Script
- Change the Text Color of CMD Every 1 Second
This article will first discuss the basic concept of a Batch script or Batch file. After introducing the Batch script, we will discuss the method for changing the text color of CMD every second using the Batch script.
Batch Script or File
A Batch script is a file containing different commands executed in a sequence. A Batch file or script stores different commands executed using the command line interpreter.
For example, the following is a Batch file hello.bat
:
@ECHO OFF
ECHO Hello World
The above Batch script contains two lines.
The first command, @ECHO OFF
, is used to hide all the commands, which are executed, from displaying on the command prompt itself. The ECHO Hello World
command displays Hello World
.
The output of the above batch script is as follows:
We can execute the .bat
file on the command line by typing the file name followed by the Enter key, as shown in the above figure. We can also run the batch file by double-clicking the file.
Batch File Extensions
A Batch script can be written in any typing or text editor with different file extensions, including .bat
, .cmd
, and .btm
.
- The
.bat
file extension is used for Batch scripts in DOS or Windows operating systems. - The
.cmd
file extension is used for Windows NT or OS/2. - The
btm
file extension is used in 4DOS or 4NT.
Change the Text Color of CMD Using Batch Script
In the Batch script, we can use a different color for text on cmd. The color
command can be used to change the text color.
For example, color [number]
is a color
command followed by the color number in hexadecimal. This hexadecimal number is like an alias to a specific color.
Consider the following script:
@echo off
color 06
echo Hello World
Output:
The above script contains a color 06
command, the hexadecimal number used as a code for the yellow color. Therefore, the said color command changes the text color of the command-line interface to yellow.
Following is the list of different available colors with their codes:
Change the Text Color of CMD Every 1 Second
We can use the color
and timeout
commands to change the text color of the cmd every unit of time. Consider the following script:
@echo off
set colors=0 1 2 3 4 5 6 7 8 9 A B C D E F
for %%n in (%colors%) do (
echo Hello World
color %%n
timeout 1 > null
)
The above script contains an array named colors
initialized with a sequence of codes for different colors, followed by a for
loop which iterates on the colors array and selects a color code in a variable n
. In the body of the for
loop, the script displays the Hello World
on screen and sleeps for 1 second using the timeout
command.
The color
command changes the text color in each iteration of the loop with different colors available in the colors
array.
The above script will change the text color of the output after each for
iteration. The outputs for some iterations of the loop are as follows: