Jan 3, 2014 02:28 AM
(This post was last modified: Jan 3, 2014 03:12 AM by ɩᴄᴀʀᴜȿ ᴌוᴠᴇѕ™.)
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.
Okay, so first we see
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
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
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
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
And at the end,
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.
My favorite Method
Last method
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 > nulOkay, so first we see
Code
MD TemporaryFolder
CD TemporaryFolderNext we see
Code
Echo Press ENTER
set /p ".=Enter the text before the colored text here" >nulThen 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"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"- Find the string "Text 1/2 to color"
- Echo something after it's found
- /A:COLORCOMBINATION /S means we want to color the found string
And at the end,
Code
CD..
RD /S /Q TemporaryFolder
Pause > nulThere 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 :__ColorMy 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 :eofLast 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


