51} How can I echo lines in different colors in NT scripts?
This FAQ item is not valid beyond 32-bit XP.
There is no easy answer. The COLOR command affects the entire
screen, not just the next output. That's not what we want in this
item. In MS-DOS+Win..95/98/ME one can use ANSI.SYS for the task. In
NT/2000/XP it also is possible, but not quite simple. Therefore, let's
start with special solutions. One option is to use "ECHOC.EXE Echo in
colors" tool for the task. It is a Turbo Pascal program included in
the tscmd.zip package. For example one might have
@echo off & setlocal enableextensions
echoc 0 14 On black bright yellow
echo.
echoc 1 13 On blue bright magenta
echo.
echo back to normal
endlocal & goto :EOF
The output would be:
C:\_D\TEST>cmdfaq
On black bright yellow
On blue bright magenta
back to normal
Another example to facilitate multiple spaces between words (and
also rainbow lines)
@echo off & setlocal enableextensions
color 0A
echoc 0 10 M
echoc 0 11 y
echoc 0 0
echoc 0 0
echoc 0 12 t
echoc 0 13 e
echoc 0 14 s
echoc 0 15 t
echo.
endlocal & goto :EOF
C:\_D\TEST>cmdfaq
My test
For a map of the colors apply
COLORMAP.CMD
If you can get hold of
QBASIC, using
colors is relatively easy and much faster. Below is an example
@echo off & setlocal enableextensions
for /f "tokens=*" %%f in ("%temp%") do set temp_=%%~sf
set skip=
findstr "'%skip%QB" "%~f0" > %temp_%\tmp$$$.bas
qbasic /run %temp_%\tmp$$$.bas
for %%f in (%temp_%\tmp$$$.bas) do if exist %%f del %%f
endlocal & goto :EOF
::
CLS 'QB
COLOR 14,0 'QB
PRINT "A simple "; 'QB
COLOR 13,0 'QB
PRINT "color "; 'QB
COLOR 14,0 'QB
PRINT "demonstration" 'QB
PRINT "By Prof. Timo Salmi" 'QB
PRINT 'QB
FOR j = 0 TO 7 'QB
FOR i = 0 TO 15 'QB
COLOR i, j 'QB
PRINT LTRIM$(STR$(i)); " "; LTRIM$(STR$(j)); 'QB
COLOR 1, 0 'QB
PRINT " "; 'QB
NEXT i 'QB
PRINT 'QB
NEXT j 'QB
SYSTEM 'QB
The output is
Best of all, however, there is a clever trick to get the ANSI.SYS
color codes in XP based on a tip in alt.msdos.batch.nt
@echo off & setlocal enableextensions
::
:: The file paths
set ConfigNT=%WinDir%\System32\config.nt
set ConfigNTbak=%WinDir%\System32\config.nt.bak
::
:: Check that you have a backup of your original CONGIF.NT
if not exist "%ConfigNTbak%" (
echo.
echo Exiting %~f0
echo "%ConfigNTbak%"
echo File not found. Needed for restoring config.nt so first run
echo copy "%ConfigNT%" "%ConfigNTbak%"
goto :EOF
)
::
:: Make a copy of the backup as a double safety
copy "%ConfigNTbak%" "%temp%\CONFIG.NT">nul
::
:: Customize CONFIG.NT to use ANSI.SYS
> "%ConfigNT%" echo DOSONLY
>>"%ConfigNT%" echo dos=high, umb
>>"%ConfigNT%" echo device=%%SystemRoot%%\system32\himem.sys
>>"%ConfigNT%" echo files=40
>>"%ConfigNT%" echo DEVICE=%WinDir%\System32\ANSI.SYS /x
::
:: Choose MS-DOS color
cls
command /cecho
<ESC>[0;40;36;1mThis should appear in cyan
<ESC>[0;40;31;1mthis in red
::
:: Restore the original CONFIG.NT
copy "%temp%\CONFIG.NT" "%WinDir%\System32\">nul
if exist "%temp%\CONFIG.NT" del "%temp%\CONFIG.NT">nul
::
:: Check if loading ANSI was successful
mem /c | find /i " ansi ">nul
if %errorlevel% GTR 0 (
echo.
echo Warning: ANSI not loaded
<Ctrl-G>
)
endlocal & goto :EOF
After successfully running the above script in the CMD.EXE ANSI
escape sequences can be used in the other scripts applying the
following format of the echo
command /cecho TheAnsiSequence + WhateverText
A demonstration with ANSI.SYS generated colors
@echo off & setlocal enableextensions enabledelayedexpansion
cls
set cecho_=command /cecho
for /l %%i in (0,1,7) do (
for /l %%j in (2,1,9) do (
set /a k=%%j-2
%cecho_%
<ESC>[%%j;%%i1H
<ESC>[4!k!;3%%im[4!k!;3%%i;1m
)
)
%cecho_%
<ESC>[40;30;1mThe end of the demo by Prof. Timo Salmi
endlocal & goto :EOF
The output is
Another, similar demo:
@echo off & setlocal enableextensions
set cecho=echo
mem /c | find /i " ansi ">nul
if %errorlevel% EQU 0 (
set cecho=command /cecho
set fg_red0=
<ESC>[0;31m
set fg_red=
<ESC>[0;31;1m
set fg_green0=
<ESC>[0;32m
set fg_green=
<ESC>[0;32;1m
set fg_yellow0=
<ESC>[0;33m
set fg_yellow=
<ESC>[0;33;1m
set fg_blue0=
<ESC>[0;34m
set fg_blue=
<ESC>[0;34;1m
set fg_magenta0=
<ESC>[0;35m
set fg_magenta=
<ESC>[0;35;1m
set fg_cyan0=
<ESC>[0;36m
set fg_cyan=
<ESC>[0;36;1m
set fg_white0=
<ESC>[0;37m
set fg_white=
<ESC>[0;37;1m
)
%cecho% %fg_red0%fg_red0 %fg_red%fg_red
%cecho% %fg_green0%fg_green0 %fg_green%fg_green
%cecho% %fg_yellow0%fg_yellow0 %fg_yellow%fg_yellow
%cecho% %fg_blue0%fg_blue0 %fg_blue%fg_blue
%cecho% %fg_magenta0%fg_magenta0 %fg_magenta%fg_magenta
%cecho% %fg_cyan0%fg_cyan0 %fg_cyan%fg_cyan
%cecho% %fg_white0%fg_white0 %fg_white%fg_white
endlocal & goto :EOF
The output is
The advantage of the CONFIG.NT method over using an external program
such as my ECHOC.EXE to output in colors is that it is faster. Note
that there are limitations to the screen display modes the method
can handle. The safest and the most obvious is to use to the 80x25
mode.