"Mastering Batch Files: Change Text Color with Echo"

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:

Change color of text after the echo in batch | Notepad++ Community

  • 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

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 file - How to echo with different colors in the Windows command ...

```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.

Change color of text after the echo in batch | Notepad++ Community

Change color of text after the echo in batch | Notepad++ Community

batch file - How to echo with different colors in the Windows command ...

batch file - How to echo with different colors in the Windows command ...

batch file - How to echo with different colors in the Windows command ...

batch file - How to echo with different colors in the Windows command ...

Can You Color Code Files In Windows at Carlos Brookover blog

Can You Color Code Files In Windows at Carlos Brookover blog

bash Echo to File: A Simple Guide to Redirect Output

bash Echo to File: A Simple Guide to Redirect Output

Customizing Your Bash Prompt in Linux: Changing Colors

Customizing Your Bash Prompt in Linux: Changing Colors

Batch Script - Echo Command - GeeksforGeeks

Batch Script - Echo Command - GeeksforGeeks

Batch file change color of specific part of text (2 Solutions!!) - YouTube

Batch file change color of specific part of text (2 Solutions!!) - YouTube

Batch File Colors: Batch Script that change Text and Background Color ...

Batch File Colors: Batch Script that change Text and Background Color ...

How to Change the Colour In Batch Files and Command Prompt - YouTube

How to Change the Colour In Batch Files and Command Prompt - YouTube

How to Change Echo Text Colors in Linux Terminal | DevOps Daily

How to Change Echo Text Colors in Linux Terminal | DevOps Daily

How To Set a Text Color in a Shell Script? | How to change the output ...

How To Set a Text Color in a Shell Script? | How to change the output ...

bash - How to change the output color of echo in Linux - Stack Overflow

bash - How to change the output color of echo in Linux - Stack Overflow

How to Change the Output Color of Echo in Linux

How to Change the Output Color of Echo in Linux

How to Change the Output Color of Echo in Linux - GeeksforGeeks

How to Change the Output Color of Echo in Linux - GeeksforGeeks

How to Change the Output Color of echo in Linux – Its Linux FOSS

How to Change the Output Color of echo in Linux – Its Linux FOSS

GitHub - TartSoft/BatColor: Command-Line Utility for Adding Colors to ...

GitHub - TartSoft/BatColor: Command-Line Utility for Adding Colors to ...

bash - How to change the output color of echo in Linux - Stack Overflow

bash - How to change the output color of echo in Linux - Stack Overflow

bash - How to change the output color of echo in Linux - Stack Overflow

bash - How to change the output color of echo in Linux - Stack Overflow

batch - How can I put 2 different texts of 1 color each in a BAT file ...

batch - How can I put 2 different texts of 1 color each in a BAT file ...