Changing CMD text color using batch script
This article will first discuss the basic concepts of batch scripts or batch files. After introducing the Batch script, let's discuss how to use the Batch script to change the text color of CMD every second.
Batch script or file
A batch script is a file that contains different commands that are executed in sequence. A batch file or script stores different commands that are executed using the command line interpreter.
For example, the following is a batch file hello.bat:
@ECHO OFF
ECHO Hello World
The batch script above contains two lines.
The first command @ECHO OFF
is used to hide all the executed commands from being displayed on the command prompt itself. ECHO Hello World
The command displays Hello World.
The output of the above batch script is as follows:
We can execute the .bat file by entering the file name in the command line and pressing Enter, as shown in the figure above. We can also run the batch file by double-clicking the file.
Batch file extension
Batch scripts 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 in Windows NT or OS/2.
- The btm file extension is used for 4DOS or 4NT.
Changing text color of CMD using batch script
In batch scripting, we can use different colors 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 for a specific color.
Consider the following script:
@echo off
color 06
echo Hello World
Output:
The script above contains a color 06 command, which is the hexadecimal number used as the code for the color yellow. Thus, the color command changes the text color of the command line interface to yellow.
Here is a list of the different available colors and 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 cmd per unit 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 called colors which is initialized with a sequence of codes for different colors, followed by a for loop which iterates over the colors array and selects the color code in the variable n. In the for loop body, the script displays Hello World on the screen and sleeps for 1 second using the timeout command.
The color command changes the text color in each iteration of the loop using a different color available in the color array.
The above script will change the text color of the output after each iteration. The output for some iterations of the loop is as follows:
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Batch set command timeout
Publish Date:2025/03/20 Views:50 Category:OPERATING SYSTEM
-
This article will first discuss the concept of timeout command in batch scripting. After that, we will discuss setting timeout command for any other command. Timeout command in batch script A timeout is a utility that pauses or delays for a
Batch merge XML files
Publish Date:2025/03/20 Views:101 Category:OPERATING SYSTEM
-
This article will first discuss and understand the XML file format. After that, we will discuss merging two or more XML files into one file using batch commands and scripts. XML File XML, also known as Extensible Markup Language, is a marku
Run .exe file from command prompt using batch script
Publish Date:2025/03/20 Views:76 Category:OPERATING SYSTEM
-
This article will show you how to use a batch (.bat) script to run a .exe type file. You can use two different commands to achieve this. Let’s discuss each method in the following sections. Run .exe file from command prompt using title an
Deleting files older than N days using batch script
Publish Date:2025/03/20 Views:57 Category:OPERATING SYSTEM
-
In this article, we will use a batch script to delete files older than N days. Deleting files older than N days using batch script The general format of the code to perform this task is shown below. FORFILES /p "D:\DIRECTORY" /S /M *.* /D -
Deleting files using batch script
Publish Date:2025/03/20 Views:178 Category:OPERATING SYSTEM
-
This article will demonstrate how to delete files using a batch script. Deleting files using batch script Generally, we can easily delete files by clicking on delete or pressing the delete button on the keyboard. But in Batch, we have to fo
Concatenate multiple files using batch script
Publish Date:2025/03/20 Views:195 Category:OPERATING SYSTEM
-
In this article, we will see how to concatenate multiple files into one file. Concatenate multiple files using batch script The general format of the commands we will use is shown below. type FileONE.txt FileTWO.txt ConcatFile.txt There are
Check if a file exists using batch processing
Publish Date:2025/03/20 Views:100 Category:OPERATING SYSTEM
-
This article will demonstrate how to use a batch script to check if a file exists through sample code. Check if a file exists using batch script The general format or syntax of the code to check if a file exists is provided below. IF EXIST
Run the batch file as administrator
Publish Date:2025/03/20 Views:142 Category:OPERATING SYSTEM
-
Batch files consist of commands that are executed by the Command Prompt. Some of these commands cannot be run without administrator privileges; therefore, it is important to run batch files as an administrator. You can manually run a batch
Running multiple files in a batch script
Publish Date:2025/03/20 Views:55 Category:OPERATING SYSTEM
-
Large scripts contain multiple files because it is easy to maintain the code. When working with larger scripts, you may want to divide them into modules to make it easier to detect any coding errors or problems. However, Batch does not have