Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch Color per line

Tags:

In batch, can you have different colors per line. For example if you had a batch file say "Hello" "How are you?"

Could you have "Hello" in blue and "How are you" in green?

(I know about the color command, and the way it colors both the background and the text)

like image 829
Gareth Jones Avatar asked Oct 28 '11 00:10

Gareth Jones


People also ask

How do I color a single line in a batch file?

Just change the parameter: call :ColorText 0c "red Text" or `call :ColorText 0a "green text"' I edited my answer to show you.

How do you change the color of text in CMD?

To set the default Command Prompt window color, select the upper-left corner of the Command Prompt window, select Defaults, select the Colors tab, and then select the colors that you want to use for the Screen Text and Screen Background.


2 Answers

You can do it without any external program. Here is something that I came over a couple of days ago. Works very good.

@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" ) echo say the name of the colors, don't read  call :ColorText 0a "blue" call :ColorText 0C "green" call :ColorText 0b "red" echo( call :ColorText 19 "yellow" call :ColorText 2F "black" call :ColorText 4e "white"  goto :eof  :ColorText echo off <nul set /p ".=%DEL%" > "%~2" findstr /v /a:%1 /R "^$" "%~2" nul del "%~2" > nul 2>&1 goto :eof 
Credits to jeb His post can be found here
like image 138
Niklas J. MacDowall Avatar answered Nov 11 '22 16:11

Niklas J. MacDowall


The Batch file below create COLORMSG.COM file (rename it as you wish) that show a message with color this way: colormsg color "Message".

@echo off ( echo e100 echo 0F B6 0E 80 00 E3 4F BF 81 00 B0 20 FC F3 AE 74 echo e110 echo 45 E3 43 8A 45 FF E8 43 00 80 3D 20 74 0E C0 E0 echo e120 echo 04 8A E0 8A 05 E8 34 00 0A C4 47 49 E3 28 32 E4 echo e130 echo 50 B0 22 F2 AE 75 1F E3 1D 8B F7 8B D1 F2 AE 75 echo e140 echo 01 41 2B D1 74 10 8B CA 5B B0 20 B4 09 CD 10 AC echo e150 echo B4 0E CD 10 E2 F9 32 C0 B4 4C CD 21 3C 61 72 02 echo e160 echo 2C 20 3C 41 72 02 2C 07 2C 30 C3 echo rcx echo 6b echo w echo q ) | debug colormsg.com > nul 

After the message the cursor is not moved to new line, so several messages may be shown in the same line. To give colors in an easier way, you may use these definitions:

set Black=0&       set Gray=8 set Blue=1&        set LightBlue=9 set Green=2&       set LightGreen=A set Aqua=3&        set LightAqua=B set Red=4&         set LightRed=C set Purple=5&      set LightPurple=D set Yellow=6&      set LightYellow=E set White=7&       set BrightWhite=F 

For example:

colormsg %Yellow% "Message in yellow  " colormsg %Blue%%BrightWhite% "Bright white text over blue background" 

This is a larger example:

@echo off setlocal EnableDelayedExpansion set i=0 for %%c in (Blk Blu Grn Aqu Red Pur Yel Whi) do (     set color[!i!]=%%c     set /A i+=1 ) for /L %%j in (0,1,7) do (     set color[!i!]=L!color[%%j]:~0,-1!     set /A i+=1 )     cls echo/ echo           BACKGROUND / FOREGROUND COMBINATIONS OF AVAILABLE SCREEN COLORS echo/ echo/ colormsg 7 "ForeGrnd:" for /L %%i in (0,1,15) do colormsg 7 " !color[%%i]!" echo/ echo BackGrnd set i=0 for /L %%b in (0,1,9) do call :ShowLine %%b for %%b in (A B C D E F) do call :ShowLine %%b echo/ goto :eof  :ShowLine colormsg 7 "   !color[%i%]!   " set /A i+=1 for /L %%f in (0,1,9) do colormsg %1%%f " %1%%f " for %%f in (A B C D E F) do colormsg %1%%f " %1%%f " echo/ 

Hey Gareth, here is a bonus for you! The following Batch file create TEXTPOS.COM file (rename it as you wish) that move the cursor to any line and column this way: textpos line col:

@echo off ( echo e100 echo 0F B6 0E 80 00 E3 2D BF 81 00 B0 20 FC F3 AE 74 echo e110 echo 23 E3 21 8D 75 FF E8 45 00 3C 20 75 3B 86 F2 E8 echo e120 echo 3C 00 86 F2 3C 20 74 04 3C 0D 75 2C 32 FF B4 02 echo e130 echo CD 10 EB 24 32 FF B4 03 CD 10 8A C6 8A CA E8 38 echo e140 echo 00 B2 20 B4 02 CD 21 8A C1 E8 2D 00 B2 0D B4 02 echo e150 echo CD 21 B2 0A B4 02 CD 21 32 C0 B4 4C CD 21 32 E4 echo e160 echo AC 3C 20 74 FB 3C 30 72 0D 3C 39 77 09 2C 30 D5 echo e170 echo 0A 8A E0 AC EB EF 8A F4 C3 D4 0A 05 30 30 8B D0 echo e180 echo 80 FC 30 74 08 86 D6 B4 02 CD 21 8A D6 B4 02 CD echo e190 echo 21 C3 echo rcx echo 92 echo w echo q ) | debug textpos.com > nul 

For example:

textpos 0 0 colormsg %Red% "Red message at top left corner of the screen" textpos 10 40 colormsg %Blue%%LightYellow% "This message start in line 10 column 40" 

EDIT

I slightly modified TEXTPOS program so it display the current cursor position if it is executed with no parameters; this feature allows to store the cursor position in a variable this way: textpos > pipe.txt & set /P pos=< pipe.txt (because textpos | set /P pos= have an error and does not work).

If a color message is shown with the same value for background and foreground colors, the text will not be visible in the screen; this feature allows to enter a password. For example, this is getpassword.bat file:

@echo off set password= for /L %%i in (1,1,%1) do set password=!password!x colormsg 7 "Enter password: " textpos > pipe.txt & set /P passpos=< pipe.txt colormsg 77 "%password%" textpos %passpos% set password= for /L %%i in (1,1,%1) do (     getakey     set password=!password!!errorlevel!     colormsg 7F "*" ) 

Previous Batch file allows to read a password with the number of characters given in the parameter, for example: call getpassword 8; entered characters are changed by its ASCII code, so the password have a rudimentary encryption. For example, to check if the entered password was "Pass":

call getpassword 4 if %password% == 8097115115 goto right_password 

I provided the GETAKEY.COM program in a previous question, but here is it again:

@echo off ( echo e100 echo B4 08 CD 21 B4 4C CD 21 echo rcx echo 8 echo w echo q ) | debug getakey.com > nul 
like image 39
Aacini Avatar answered Nov 11 '22 17:11

Aacini