Copyright © 2003- by Prof. Timo Salmi  
Last modified Sun 15-May-2016 16:18:37

Assorted NT/2000/XP/.. CMD.EXE Script Tricks
From the html version of the tscmd.zip 1cmdfaq.txt file
To the Description and the Index

This page is edited from the 1cmdfaq.txt faq-file contained in my tscmd.zip command line interface (CLI) collection. That zipped file has much additional material, including a number of detached .cmd script files. It is recommended that you also get the zipped version as a companion.

Please see "The Description and the Index page" for the conditions of usage and other such information.

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
  echoc 1 13 On blue bright magenta
  echo back to normal
  endlocal & goto :EOF

The output would be:
  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
  endlocal & goto :EOF

  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
  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
  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
  NEXT j '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 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

  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 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
  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.

References/Comments: (If a Google message link fails try the links within the brackets.)
  Google Groups Jan 11 2004, 8:23 pm [M]
  Google Groups Jul 12 2004, 8:46 pm [M]
  Google Groups May 4 2007, 2:55 pm [M]
  Google Groups Nov 2 2007, 5:02 pm [M]
  Google Groups Dec 30 2009, 7:12 pm [T]
  ANSI escape code Wikipedia
  ansi.sys color codes Google search
  GitHub - adoxa/ansicon: Process ANSI escape sequences for Windows console programs