Read Mode
Default

How to change text color of CMD? | CMD Color Text | Multiple Colors in CMD

In Every Batch Programmer's Life, There was the time - when they realize the Limited capabilities of CMD/DOS. And, One of those limitations is having Multiple Colors text in CMD. And, if you are going through a similar kind of Emotion, then you are in the right place. Let me share my knowledge of Batch with you via TheBATeam Platform to Change the text color of CMD.

Most programming languages have a fairly simple way to display colored text. From Powershell to HTML to C# colored text isn't that hard. But, there isn't an easy way to do it in batch. The only option you have is using the color command (DOS Color Codes) to change the color of the entire screen. But, there are alternatives. Scripts shared on stack Overflow, as well as some third party plugins you can use, can make it quite easy to add multiple colors to your Batch files these are scripts we use at TheBATeam, and now you can too!


How to change text color of CMD? | CMD Color Text

There are some methods that you can use to achieve, Multiple Colors in CMD. Here, I have researched various sources to find the Best method and Now, I am sharing them with all of You. Let's Have a look at them one by one.

Way #1: Using Findstr color Function to Get Multiple Color Text

This method is the most widely used, and the most error-prone way of offering colored text to your scripts. The best version I was able to find is by a user on stack-overflow named Jeb, which is written below. It is written in pure batch using the findstr command. Here is an example file:


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

rem Prepare a file "X" with only one dot
<nul > X set /p ".=."

call :color 1a "a" /n
call :color 1b "b"
call :color 1c "^!<>&| %%%%"*?"
pause
exit /b

:color
set "param=^%~2" !
set "param=!param:"=\"!"
findstr /p /A:%1 "." "!param!\..\X" nul
<nul set /p ".=%DEL%%DEL%%DEL%%DEL%%DEL%%DEL%%DEL%"
if not "%~3"=="/n" echo.
exit /b
REM Color Function Created by Jeb https://stackoverflow.com/users/463115/jeb

The key parts are the first eight lines that must be at the top of your file and the bottom six lines. This allows you to create colored text using the default color codes (you can see them with the color /? command). The way to do it is:
call :color 0a "text" /n 
Where 0a is the color code you want to use, "text" is the text you want to print on the screen and /n is an optional parameter you can add to make the function not create a new line.

Downsides of findstr function:

  • It does not work if you run your script as administrator on Windows 10
  • It can cause problems with complicated scripts
  • It can cause problems with very long scripts
  • It requires you to have an empty document called "x" in your folder.

Way #2: Windows 10 color function

This method requires you to have Windows 10 Threshold 2 November 18, 2015 (10.0.10586.11) or later installed on your system. It uses a NEW COLOR SYSTEM similar to How Bash Scripts Get Colors. This system takes some getting used to, unless you use the following script written by Lucas Elliott with IT Command on Github:

:color
Rem use: call :color HexColorCodeLikeInColorCommand "Text to say" /u(underline) /n(Does no create new like) setlocal EnableDelayedExpansion set "text=%~2" set color=%1 set FG=%color:~-1% set BG=%color:~0,1% set Sameline=False set Underline=False if /i "%~3"=="/n" set Sameline=True if /i "%~3"=="/u" set Underline=True if /i "%~4"=="/n" set Sameline=True if /i "%~4"=="/u" set Underline=True if /i "%FG%"=="0" set c1=30 if /i "%FG%"=="1" set c1=34 if /i "%FG%"=="2" set c1=32 if /i "%FG%"=="3" set c1=36 if /i "%FG%"=="4" set c1=31 if /i "%FG%"=="5" set c1=35 if /i "%FG%"=="6" set c1=33 if /i "%FG%"=="7" set c1=37 if /i "%FG%"=="8" set c1=90 if /i "%FG%"=="9" set c1=94 if /i "%FG%"=="a" set c1=92 if /i "%FG%"=="b" set c1=96 if /i "%FG%"=="c" set c1=91 if /i "%FG%"=="d" set c1=95 if /i "%FG%"=="e" set c1=93 if /i "%FG%"=="f" set c1=97 if /i "%BG%"=="0" set c2=40 if /i "%BG%"=="1" set c2=44 if /i "%BG%"=="2" set c2=42 if /i "%BG%"=="3" set c2=46 if /i "%BG%"=="4" set c2=41 if /i "%BG%"=="5" set c2=45 if /i "%BG%"=="6" set c2=43 if /i "%BG%"=="7" set c2=47 if /i "%BG%"=="8" set c2=100 if /i "%BG%"=="9" set c2=104 if /i "%BG%"=="a" set c2=102 if /i "%BG%"=="b" set c2=106 if /i "%BG%"=="c" set c2=101 if /i "%BG%"=="d" set c2=105 if /i "%BG%"=="e" set c2=103 if /i "%BG%"=="f" set c2=107 if %SameLine%==True call :NoNewLine & endlocal & exit /b if %Underline%==False echo [%c1%m [%c2%m%Text% [0m if %Underline%==True echo [4m [%c1%m [%c2%m%Text% [0m endlocal exit /b :NoNewLine if %Underline%==False echo|set /p=" [%c1%m [%c2%m%Text% [0m" if %Underline%==True echo|set /p=" [4m [%c1%m [%c2%m%Text% [0m" endlocal exit /b REM Created by Lucas Elliott with IT Command https://github.com/ITCMD/BatchColor
This script is similar to the findstr function in that you call it the exact same way. However, this version will run fine in administrator, and allows you to underline text with the /u option!
Example:
call :color 0a "underlined text" /n /u
     All you have to do is copy the above code to the bottom of your batch file, there are no required lines at the top. It also does not create any files in your folder.

Downsides of Windows 10 Color Function:

  • It requires you to have Windows 10 (This can be solved with a solution mentioned later).
  • It is slightly slower than the findstr function

Way #3: Use Batbox Plugin to Have Multiple Colors in CMD

This option uses Batbox.exe - A batch plugin which is a lot quicker than others, and only requires you to have the file in a callable directory. You can change the color with the following command:
Batbox /c 0x0a 
In this example, the 0x tells the batbox plugin that you are using the default Batch Color Codes, and the 0a is the color code in hex. Now, all the text after this command will come out as the color you wanted. This Runs quickly and easily but it requires you to tell it when to switch back to the original code.

Downsides of Batbox Plugin

  • It requires for the file to be put in the working folder (this can be solved using IT Commands Into-Batch program found here: https://github.com/ITCMD/Into-Batch)
  • It can set off sensitive antiviruses and/or Windows Smartscreen (one way to test if an antivirus is installed is with the command: 



Combining way #1 and #2:

This automatically selects whether to use the findstr or the Windows 10 Edition (good for Windows 7 versions). You can add this to the top of your file to choose which version you want to use:


@echo off
set type=none
FOR /F "tokens=4-7 delims=[]. " %%i in ('ver') DO (
  SET /A _MajorMinor = %%i * 100 + %%j
  set /A _Build      = %%k0 /10
  set /A _Revision   = %%l0 /10
)
set "_AE="        AE like Ansi Escape
IF %_MajorMinor% GEQ 1000 if %_Build% GTR 10586 ( set "_AE=Yes" ) else ( 
      if %_Build% EQU 10586 if %_Revision% GEQ 11 set "_AE=Yes" )
rem use the _AE flag
if defined _AE (set type=:Color10) ELSE (set type=:Color8m)

if %type%==:Color10 goto StartOfFile
setlocal EnableDelayedExpansion
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
  set "DEL=%%a"
)
rem Prepare a file "X" with only one dot
<nul > X set /p ".=."
goto StartOfFile

:Color10
setlocal EnableDelayedExpansion
set "text=%~2"
set color=%1
set FG=%color:~-1%
set BG=%color:~0,1%
set Sameline=False
set Underline=False
if /i "%~3"=="/n" set Sameline=True
if /i "%~3"=="/u" set Underline=True
if /i "%~4"=="/n" set Sameline=True
if /i "%~4"=="/u" set Underline=True


if /i "%FG%"=="0" set c1=30
if /i "%FG%"=="1" set c1=34
if /i "%FG%"=="2" set c1=32
if /i "%FG%"=="3" set c1=36
if /i "%FG%"=="4" set c1=31
if /i "%FG%"=="5" set c1=35
if /i "%FG%"=="6" set c1=33
if /i "%FG%"=="7" set c1=37
if /i "%FG%"=="8" set c1=90
if /i "%FG%"=="9" set c1=94
if /i "%FG%"=="a" set c1=92
if /i "%FG%"=="b" set c1=96
if /i "%FG%"=="c" set c1=91
if /i "%FG%"=="d" set c1=95
if /i "%FG%"=="e" set c1=93
if /i "%FG%"=="f" set c1=97

if /i "%BG%"=="0" set c2=40
if /i "%BG%"=="1" set c2=44
if /i "%BG%"=="2" set c2=42
if /i "%BG%"=="3" set c2=46
if /i "%BG%"=="4" set c2=41
if /i "%BG%"=="5" set c2=45
if /i "%BG%"=="6" set c2=43
if /i "%BG%"=="7" set c2=47
if /i "%BG%"=="8" set c2=100
if /i "%BG%"=="9" set c2=104
if /i "%BG%"=="a" set c2=102
if /i "%BG%"=="b" set c2=106
if /i "%BG%"=="c" set c2=101
if /i "%BG%"=="d" set c2=105
if /i "%BG%"=="e" set c2=103
if /i "%BG%"=="f" set c2=107

if %SameLine%==True call :NoNewLine & endlocal & exit /b

if %Underline%==False echo  [%c1%m [%c2%m%Text% [0m
if %Underline%==True echo  [4m [%c1%m [%c2%m%Text% [0m
endlocal
exit /b

:NoNewLine
if %Underline%==False echo|set /p=" [%c1%m [%c2%m%Text% [0m"
if %Underline%==True echo|set /p=" [4m [%c1%m [%c2%m%Text% [0m"
endlocal
exit /b
:color8m
set "param=^%~2" !
set "param=!param:"=\"!"
findstr /p /A:%1 "." "!param!\..\X" nul
<nul set /p ".=%DEL%%DEL%%DEL%%DEL%%DEL%%DEL%%DEL%"
if not "%~3"=="/n" echo.
exit /b
:none
echo %~2
exit /b
:StartOfFile
::===============================================================================================
call %type% 0a "Hello there. This has automatically selected the best version."
call %type% 0a "If " /n
call %type% 0f "THIS" /n /u
call %type% 0a " Text is underlined you are running a system compatable with windows 10"
pause
You can remove everything under the :=========================== part. It is just there as an example. What this script does is automatically detect the version of Windows it is running on. If compatible, it will use the Windows 10 version. If not, it will use the findstr function. The only difference is that instead of using the command call :color  you use the command call %type%.

Summary:

    All in all, which one you use depends on the situation you're in. If you want the easiest version for a situation you control, USE BATBOX. If you plan to distribute your code and want it to work on all systems use the Windows 10 function, or, the findstr function if using systems before Windows 10. I hope that you will use these functions to improve your scripts and bring your windows from black and white to full color! Batch on my friends -Lukaka USA! Signing OFF.
Submit Your Batch Projects to Publish At TheBATeam

No comments:

Powered by Blogger.