How do I make one particular line of a batch file a different color then the others?
I'm working on a program which generates the day's weather for D&D games. I want the program to display a warning in red text when a storm is generated so the DM is aware that this weather is not typical. To reduce the number of keystrokes, it must do this on the same screen as the text detailing the weather itself.
Currently for a storm's entry I have this:
:des4a
cls
type c:\AutoDM\WeatherGen\data\forcast1.txt
color 0c
echo There is an Ashstorm!
color 0a
echo.
echo It is %temp% degrees and the sky is invisible threw the thick
echo billowing clouds of ash.
echo.
echo # Survival checks to light a fire are at +15.
echo # Small unprotected flames will be snuffed out.
echo # Non-firearm ranged attacks are at a -8 to hit.
echo # Preception checks take a -10 for every 10 feet of distance.
echo # Survival checks to get along in the wild are at +15.
echo # Stealth checks are at a +5.
echo.
echo SPECIAL!
echo.
set /a die=6
set /a inches=%random%%%die+1
echo # The ashstorm will deposit %inches% inches of ash durring its
echo durration.
echo # Tracking a target after an ashstorm is at a +15.
type c:\AutoDM\WeatherGen\data\forcast2.txt
echo.
echo.
pause
goto menu
The type commands are calling text documents which contain a header and footer for each entry to help the program look professional and provide a border to assist with word wrap. Thy cannot be removed. DO not suggest something which would make me unable to use the type commands as they currently exist please. Please understand that this red text line will be added to a lot of different parts of the program, each time there is a storm for each and every biome in the generator. I would prefer it to not be more then just 2 or 3 lines of code (But if there is only one way to do it well...)
Can I have the one line in red and the rest in green? Or should I have the program beep to call attention to the fact? Do you have a better and simpler idea? How do I do this?
Solution 1:
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
Solution 2:
Script from here: How to have multiple colors in a Windows batch file? a little modified (simplyfied):
@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 next line in another color:
call :ColorText 0c "There is an Ashstorm!"
echo this was red.
call :ColorText 0a "you survived it."
goto :eof
:ColorText
echo off
echo %DEL% > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1
goto :eof
It was a link in the very first answer of "possible duplicate: How to change the text color of comments line in a batch file "