Hack Forums
[Shell] Easy multiple colors in batch - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Shell] Easy multiple colors in batch (/showthread.php?tid=3971002)



Easy multiple colors in batch - ɩᴄᴀʀᴜȿ ᴌוᴠᴇѕ™ - Jan 3, 2014

I'll be explaining a much simpler way to have multiple color text in batch.

First a quick look at the code before I explain it.
Code
@Echo off

MD TemporaryFolder
CD TemporaryFolder
Echo Press ENTER
set /p ".=Enter the text before the colored text here" >nul

set /p ".=Text after colored text 1" <nul >"Text 1 to color"
set /p ".=Text after colored text 2" <nul >"Text 2 to color"

findstr /A:0E /S /C:"Text after colored text 1" "Text 1 to color"
Echo.
findstr /A:0c /S /C:"Text after colored text 2" "Text 2 to color"

echo.

CD..
RD /S /Q TemporaryFolder
Pause > nul

Okay, so first we see
Code
MD TemporaryFolder
CD TemporaryFolder
This basically says "Make directory named TemporaryFolder and "Change Directory to TemporaryFolder. We do this because of the FINDSTR command, but I'll explain that later.

Next we see
Code
Echo Press ENTER
set /p ".=Enter the text before the colored text here" >nul
These 2 lines of code just tell the user to "Press enter". We do this because inputting any information into the variable %.% would be pointless as it is >nul 'd out.

Then you see
Code
set /p ".=Text after colored text 1" <nul >"Text 1 to color"
set /p ".=Text after colored text 2" <nul >"Text 2 to color"
These 2 lines of code actually put the text "Text after colored text 1/2" into a temporary file called "Text 1/2 to color"

Finally we see the bread and butter of this script. This is the main piece of this script to give it the color
Code
findstr /A:0E /S /C:"Text after colored text 1" "Text 1 to color"
Echo.
findstr /A:0c /S /C:"Text after colored text 2" "Text 2 to color"
So we first see the command "findstr" which stands for Find string (for those who didn't know). What we want it to do is
  1. Find the string "Text 1/2 to color"

  2. Echo something after it's found

  3. /A:COLORCOMBINATION /S means we want to color the found string



And at the end,
Code
CD..
RD /S /Q TemporaryFolder
Pause > nul
What happens here is we use CD.. to revert back to the previous directory, RD means Remove Directory "TemporaryFolder" and finally pause to keep our script from crashing.

There are multiple ways of coloring text as I've posted in the batch section, but this is by far the simplest. I hope you all enjoyed this :) PM me if you have any questions!



Another method of multi colored text.
Code
@Echo Off
Call :Color A "##" \n E "" C " 21 " E "!" \n B "##" \n
Pause >Nul
Exit /B

:Color
Subst `: "!Temp!" >Nul &`: &Cd \
SetLocal DisableDelayedExpansion
If Not Exist `.7 (
Echo(|(Pause >Nul &Findstr "^" >`)
Set /P "=." >>` <Nul
For /F "delims=;" %%# In (
'"Prompt $H;&For %%_ In (_) Do Rem"') Do (
Set /P "=%%#%%#%%#" <Nul >`.3
Set /P "=%%#%%#%%#%%#%%#" <Nul >`.5
Set /P "=%%#%%#%%#%%#%%#%%#%%#" <Nul >`.7))
:__Color
Set "Text=%~2"
If Not Defined Text (Set Text=^")
SetLocal EnableDelayedExpansion
Set /P "LF=" <` &Set "LF=!LF:~0,1!"
For %%# in ("!LF!") Do For %%_ In (
\ / :) Do Set "Text=!Text:%%_=%%~#%%_%%~#!"
For /F delims^=^ eol^= %%# in ("!Text!") Do (
If #==#! EndLocal
If \==%%# (Findstr /A:%~1 . \` Nul
Type `.3) Else If /==%%# (Findstr /A:%~1 . /.\` Nul
Type `.5) Else (Echo %%#\..\`>`.dat
Findstr /F:`.dat /A:%~1 .
Type `.7))
If "\n"=="%~3" (Shift
Echo()
Shift
Shift
If ""=="%~1" Goto :Eof
Goto :__Color

My favorite Method
Code
@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"
)

Call :ColorText 0A "Hello"
Call :ColorText 0C " World" & Echo.
Call :ColorText 0E "New line"
Pause > nul
Goto :Eof

:ColorText
echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1
goto :eof

Last method
Code
@echo off
echo These are the colored texts:
echo.
call :ColorText 0a "GREEN"
set /p ".= " <nul
call :ColorText 0c "RED"
set /p ".= " <nul
call :ColorText 09 "BLUE"
set /p ".= " <nul
call :ColorText 0d "PINK"
set /p ".= " <nul
call :ColorText 0e "YELLOW"
set /p ".= " <nul
call :ColorText 08 "GRAY" end
echo.
echo.

set /p ".=Text before " <nul
call :ColorText 0a "GREEN"
set /p ".= and between green and " <nul
call :ColorText 0c "RED" end
echo. and after red.
echo.
echo.
pause
exit

:: Keep this label exactly as it is and do not change anything here!
:ColorText [%1 = Color] [%2 = Text]
set /p ".=." > "%~2" <nul
findstr /v /a:%1 /R "^$" "%~2" nul 2>nul
set /p ".=" <nul
if "%3" == "end" set /p ".=  " <nul
del "%~2" >nul 2>nul
exit /b



* - Options - Jan 3, 2014

Thanks! I will implement this in Cookie Clicker just for fun :) I was looking for this kind of explanation before but I couldn't find it so I did not add colors to my scripts. :/


* - ɩᴄᴀʀᴜȿ ᴌוᴠᴇѕ™ - Jan 3, 2014

(Jan 3, 2014 03:31 AM)Options Wrote:  Thanks! I will implement this in Cookie Clicker just for fun :) I was looking for this kind of explanation before but I couldn't find it so I did not add colors to my scripts. :/

Well you should know that you can always pm me for help ^-^ I always put it in my threads


* - Nakamoto. - Jan 3, 2014

Nice job, you have definitely improved from when you first joined. You have some amazing tutorials and you are very knowledgeable in batch now. This is just another one of your great guides.


* - ɩᴄᴀʀᴜȿ ᴌוᴠᴇѕ™ - Jan 4, 2014

(Jan 3, 2014 09:40 PM)RenRioku™ Wrote:  Nice job, you have definitely improved from when you first joined. You have some amazing tutorials and you are very knowledgeable in batch now. This is just another one of your great guides.

[img]http://cdn.alltheragefaces.com/img/faces/png/happy-oh-stop-it-you.png[/img]


* - Recoome - Jan 31, 2015

Thanks for this I write my own experimental batch scripts for ehmm.. experimental purposes and this could really help out! Thanks


* - SIM-1895736 - Jan 31, 2015

Thank you for this mate. I'm writing a simple batch programe now a days and will insert this too (just for fun ;)


* - nsevyd - Aug 28, 2015

This is a very nice way of doing it, thanks!


* - oramck - Sep 7, 2015

Greaat work, Tks for sharing this!


* - en3rgizz - Sep 29, 2015

Greaat work, its verry good tutorial.


* - Seymour_Birkhoff - Oct 3, 2015

i just learned how to make batch files and was very curious as to how to do this. i even tinkered around hoping i would find a fix.


* - DataBuster - Jan 20, 2017

I personally always rename ":ColorEcho" to ":color", that's way quicker to type.