I was having the same problem yesterday, I did some research and this worked for me.
EDIT: This is NOT the same code as the other one.
@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 :colorEcho 0a "This is colored green with a black background!"
echo.
call :colorEcho a0 "This is colored black with a green background!"
echo.
pause
exit
:colorEcho
echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1i
it does this:
All you need to do to fit this is put the part from SETLOCAL EnableDel
… to )
to the beginning of your code (If your code starts with @echo off then put it under that) and also put the part from :colorEcho
to del %~2
at the exact bottom of your script (NOTHING UNDERNEATH!)
Now between you notice
call :colorEcho 0a "This is colored green with a black background!"
echo.
call :colorEcho a0 "This is colored black with a green background!"
echo.
pause
exit
Explained line by line:
First line (call :colorEcho 0a "This is colored green with a black background!"
): This is a colored
echo, it suprisingly says This is colored green with a black background!
in 0a
(Black Bg, Green Text)
Second line (echo.
) prints a newline, since our colored echo doesn’t print one.
SO
Let’s say you wanted to say “Hello World” in Hello
being yellow and World
being green.
Example!
@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 :colorEcho 0e "Hello "
call :colorEcho 0a " World"
pause
exit
:colorEcho
echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1i
This is what it does:
I hope this is understandable!
EDIT: Make sure the program exit
s before it could reach :colorEcho