Easy ANSI and PROMPT String Processing
Here is an approach that greatly simplifies the generation and display of ANSI control strings and the functions that use PROMPT dollar sign characters.
  Putting the PROMPT Command Through Its Paces
The fundamentals of the process are pretty much old-hat, but I have never seen it implemented in a generalized procedure before. First, let's look at the procedure itself and then at some of the myriad of things it can be used for.
:: ANSI.BAT - Makes using ANSI control sequences easy. :: Tom Lavedas <lavedas@pressroom.com> :: http://www.pressroom.com/~tglbatch/ @echo %dbga% off > %ram%.\}A{.bat echo @prompt %1 > %ram%.\}B{.bat %comspec% /e:2048/c %ram%.\}A{.bat for %%v in (%ram%.\}B{.bat del) do call %%v %ram%.\}?{.bat :: A line needed to surpress a line feed from the FOR
The idea is to use the PROMPT command to translate characters that are normally hard to generate into the easily used equivalent dollar sign representations, such as $E for the escape character. Using ANSI.BAT, setting the DOS display screen to a blue background with yellow text foreground is as simple as ...

  call ansi echo.$e[1m$e[33m$e[44m$e[A

BTW, the $e[A string tacked on the end is an ANSI control code to keep the cursor from advancing (in most cases) when the command is executed. Note that only one command line input argument is allowed.

Considering the example shown above, the ANSI control string could be <Esc>[1;33;44m, except that the semicolons will act as delimiters truncating the string passed to the routine at the first semicolon. Fortunately, this was easily avoided in the example by specifying each of the three parts of the color definition; intensity, foreground and background; sequentially. Alternatively, the shorter string (with the dollar sign substitution) can be stored in an environment variable and then passed by name to the ANSI routine, something like this ...

  set Color=$e[1;33;44m
call ansi echo.%%Color%%$e[A
set Color=>nul

The example above also illustrates an interesting capability of ANSI.BAT -- environment variables can be passed by name, even from the DOS command line. That is, when the percent signs are doubled up in a batch file call, or used singly on the command line, the string between the percents will be passed to the routine, not the contents of the variable. This use is further illustrated in the demonstration routine presented below. In it, a FOR statement's variable replaces the literal variable name.

The passing of a single command line argument may seem very restrictive. However, I believe that rather than being restrictive, it makes ANSI.BAT applicable to many more situations while also enhancing its robustness. Because there are so many characters that act as delimiters on a DOS command line, the issue of how to assemble multiple passed arguments is not a trivial matter. For example, should each piece be concatenated with spaces, semicolons, commas or equal signs between them? It just seemed safest to restrict the input to the one argument, then there is no confusion.

Besides, the apparent limitation is fairly easily overcome with a little cleverness. For example, the equal sign can often be substituted for a space in a DOS command without changing its meaning. Thus, to use ANSI.BAT to store the current time into an environment variable named TIME, the dollar sign equivalent to the equal sign ($Q) can be substituted for the space normally needed between the SET and the variable name. The call could be constructed as ...

  call ansi set$QTime$Q$T

The command line input is built without an unwanted delimiter. The PROMPT command expands the input string to ...

  set=Time=13:38:27.37

for example. When this line; stored in the secondary batch file, }B{; is invoked by the subsequent FOR command, it correctly places the current time into the environment as the variable, Time.

  A Demonstration
The following batch file, ANSIDEMO.BAT, demonstrates a number of uses of ANSI.BAT as well as several tricks that can be applied.
:: ANSIDEMO.BAT - Demonstrates some of the uses of ANSI.BAT :: Tom Lavedas <lavedas@pressroom.com> :: http:://www.pressroom.com/~tglbatch/ :: Revised: 19 Oct 98 (Correct the setting of Function keys.) :: Exploiting the PROMPT dollar sign symbols, :: $T - Time, :: $D - Date, :: $V - Version, :: $P - Directory, and :: $N - Drive :: ANSI.SYS must be loaded to use the ANSI control strings (i.e. colors), :: but not the PROMPT dollar sign parameters. @echo off set Time=Time: $e[80D$e[12C$e[36m$t$h$h$h set Date=Date: $e[80D$e[12C$e[36m$d set Ver=Version: $e[80D$e[12C$e[36m$v set Dir=Directory:$e[80D$e[12C$e[36m$p set Drv=Drive: $e[80D$e[12C$e[36m$n$_echo. :: :: Environment variables passed by name :: for %%v in (Time Date Ver Dir Drv) do call ansi echo.%%%%v%%$e[33m :: :: Exploiting the PROMPT dollar sign variables, $T, $D, $V, $P and $N :: :: A mini-subroutine stored in an environment variable! :: set Date=echo set Day$q%%%%1$g %ram%.\{t}.bat$_ set Date=%Date%echo set Date$q%%%%2$g$g%ram%.\{t}.bat$_ set Date=%Date%call %ram%.\{t} $d$_ set Date=%Date%del %ram%.\{t}.bat call ansi %%Date%% :: :: See, a single input argument isn't really too restrictive. :: But, you better count those percent signs carefully. :: call ansi for$Q%%%%%%%%v$Qin$Q($v)$Qdo$Qset$QVer$q%%%%%%%%v :: :: How about three in one! :: call ansi set$QTime$q$t$_set$QDir$q$p$_set$QDrv$q$n :: echo.Time: %Time% echo Day: %Day% echo.Date: %Date% echo.Version: %Ver% echo.Directory: %Dir% echo.Drive: %Drv% echo. for %%v in (Time Day Date Ver Dir Drv) do set %%v=>nul :: :: Redifine the F10 key to issue a CLS command and the F12 key to :: issue an EXIT. ANSI.SYS must be loaded. (corrected 19 Oct 98) :: set Kset=echo.$e[0;68;27;"CLS";13p$e[0;134;27;"Exit";13pFunction keys set. call ansi %%Kset%% set Kclr=echo.$e[0;68;0;68p$e[0;134;0;134p
The output from the demo looks something like this ...

  Time:      17:27:46
Date:      Mon 10-19-1998
Version:   MS-DOS Version 6.20
Directory: C:\TGLBATCH\BATCH
Drive:     C

Time:      17:27:47.48
Day:       Mon
Date:      10-19-1998
Version:   6.20
Directory: C:\TGLBATCH\BATCH
Drive:     C

Function keys set.
  More Information
For more information on the PROMPT dollar sign parameters or ANSI control strings check out the DOS online HELP command or for Windows 95 users see the PROMPT link or the ANSI.SYS link. These simple text files are the output from HELP on these two subjects.



Top | General Notes | Homepage