In the realm of Windows command line scripting, batch files are a powerful tool for automating tasks and streamlining workflows. One of the many ways to enhance the user experience and improve readability of these scripts is by changing the text color of the output. This can be achieved using the color command or the echo command with specific color codes. In this article, we will delve into the intricacies of changing the echo text color in batch files.
Understanding the color Command
The color command in batch files is a straightforward way to change the text color of the command prompt. It takes a single argument, which is a two-character code representing the desired color. For instance, color 0A will set the text color to green, while color F0 will set it to white on a black background.
Here's a list of some commonly used color codes for the color command:

0A- Green0B- Light Green0C- Aqua0D- Light Aqua0E- Red0F- Light Red10- Dark Blue11- Light Blue12- Purple13- Light Purple14- Yellow15- White16- Gray17- Light Gray18- Light Blue19- Light Green1A- Light Aqua1B- Light Red1C- Light Purple1D- Yellow1E- High Intensity1F- Default
Using echo with Color Codes
While the color command changes the text color for the entire command prompt, you can also change the color of specific text output using the echo command with color codes. This is particularly useful when you want to highlight certain parts of your output or create more visually appealing scripts.
The color codes for echo are slightly different and are prefixed with a ^ character. Here are the same color codes as above, but with the ^ prefix:
^0A- Green^0B- Light Green^0C- Aqua^0D- Light Aqua^0E- Red^0F- Light Red^10- Dark Blue^11- Light Blue^12- Purple^13- Light Purple^14- Yellow^15- White^16- Gray^17- Light Gray^18- Light Blue^19- Light Green^1A- Light Aqua^1B- Light Red^1C- Light Purple^1D- Yellow^1E- High Intensity^1F- Default
Example: Using echo with Color Codes
Let's say you want to create a batch file that outputs a list of files in the current directory, with directories in green and files in red. Here's how you can do it:

```batch @echo off dir /b /a-d /o:gn > temp.txt for /f "tokens=*" %%a in (temp.txt) do ( echo ^^0E[%%a] ) del temp.txt pause ```
In this example, the dir command is used to list all files in the current directory, and the output is redirected to a temporary file called temp.txt. The for loop then reads each line of the temporary file and echoes it with the desired color code.
Changing Text Color Dynamically
In some cases, you might want to change the text color dynamically based on certain conditions. This can be achieved using if statements or switch statements in batch files. For instance, you can change the text color to red when an error occurs, or to green when a task is completed successfully.
Here's an example that demonstrates this concept:
```batch @echo off set "error=false" if "%error%"=="true" ( echo ^^0EError occurred! ) else ( echo ^^0ATask completed successfully! ) pause ```
In this example, the if statement checks the value of the error variable. If it's set to true, the text color is changed to red and an error message is displayed. If the variable is false, the text color is changed to green and a success message is displayed.
Conclusion
Changing the echo text color in batch files is a powerful technique that can greatly enhance the readability and usability of your scripts. Whether you're using the color command to change the text color of the entire command prompt, or using echo with color codes to highlight specific text, there are numerous ways to leverage this feature to create more engaging and informative scripts.
In this article, we've explored the basics of changing the echo text color in batch files, and provided several examples to illustrate how this can be achieved. By mastering these techniques, you'll be well on your way to creating more polished and professional batch files that are a joy to use.