Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2002
    Location
    Bay Area
    Posts
    511

    DOS Batch file question

    I was not able to find an example or work out a solution to this. Is it possible to
    refer to a variable in a DOS batch file using a compound reference to the variable?
    Here is non-working batch file code and I would like to display the color names,
    as I attempted to do in the last line of code.

    Thanks
    Code:
    Echo Off
    Set c0 = Black
    Set c1 = Blue
    Set c2 = Green
    Set c3 = Aqua
    Set c4 = Red
    Set c5 = Purple
    Set c6 = Yellow
    Set c7 = White
    Set c8 = Gray
    Set c9 = Light Blue
    Set cA = Light Green
    Set cB = Light Aqua
    Set cC = Light Red
    Set cD = Light Purple
    Set cE = Light Yellow
    Set cF = Bright White
    
    Set bg=F
    Set fg=0
    
    COLOR %bg%%fg%
    
    Echo Background color is c%bg%, foreground color is c%fg%

  2. #2
    Join Date
    Jan 2003
    Location
    Massachusetts
    Posts
    5,800
    I think you are looking for Dereferencing variables in DOS. Have you come across this article?

  3. #3
    Join Date
    Jan 2002
    Location
    Bay Area
    Posts
    511
    Thank you MCrowley for that link. It includes DOS batch file programming information about SETLOCAL ENABLEDELAYEDEXPANSION
    and using the ! mark to reference a variable by putting a literal and another variable together, such as !c%var%!, which is the
    literal “c” and whatever value is assigned to the variable var.

    I wanted to know if there was an alternative to doing a lookup without using a bunch of IF statements, so now I know.
    I did locate and try the map function for doing lookups, and it worked too, but I am clueless about what the map code does
    and have not seen an explanation of how it works.

    Here is my test code that uses the dereferencing of variables to look up color names for display when the DOS window background
    and foreground colors are changed via a prompt in this batch file. This may appear to be of little use, but I am adapting this utility to
    store the colors choice in a file to add color to a vbScript application I am working on, which runs in a DOS window.

    Thanks again.
    Code:
    @Echo Off
    SETLOCAL ENABLEDELAYEDEXPANSION
    
    Set infoMessage=
    
    Set c0=Black & Set c1=Blue & Set c2=Green & Set c3=Aqua
    Set c4=Red & Set c5=Purple & Set c6=Yellow & Set c7=White
    Set c8=Gray & Set c9=Light Blue & Set cA=Light Green & Set cB=Light Aqua
    Set cC=Light Red & Set cD=Light Purple & Set cE=Light Yellow & Set cF=Bright White
    
    :Top
    
    Call:getInputFunc %infoMessage%
    
    If .%colorspecs%==. GoTo Done
    
    Set STRING=%colorspecs%
    Set STRING=%STRING:a=A%
    Set STRING=%STRING:b=B%
    Set STRING=%STRING:c=C%
    Set STRING=%STRING:d=D%
    Set STRING=%STRING:e=E%
    Set STRING=%STRING:f=F%
    
    Set char1=%STRING:~0,1%
    Set char2=%STRING:~1,2%
    
    If .%char1%==.%char2% Set infoMessage=%char1%%char2% not allowed. Characters must be from those listed and unique. & GoTo Top
    
    Call:myValidateFunc
    
    If .%char1%==. Set infoMessage=Your entry was not a valid combination. You entered %colorspecs% & GoTo Top
    
    COLOR %char1%%char2%
    
    Set bg=!c%char1%!
    Set fg=!c%char2%!
    
    Set infoMessage=(%char1%%char2%)   background:  %bg%     foreground:  %fg%
    
    GoTo Top
    
    :Done
    
    endlocal
    GoTo:eof
    
    ::--------------------------------------------------------
    ::-- Function section
    ::--------------------------------------------------------
    
    :getInputFunc
    
    cls
    Echo. %*
    Echo.
    Echo.  +------------DOS Window Color Setting-------------+
    Echo.  ^|                                                 ^|
    Echo.  ^|  Type in color codes to view different          ^|
    Echo.  ^|  combinations of DOS window color (background)  ^|
    Echo.  ^|  and text color (foreground).                   ^|
    Echo.  ^|                                                 ^|
    Echo.  ^|  Color options:                                 ^|
    Echo.  ^|                                                 ^|
    Echo.  ^|      0 = Black       8 = Gray                   ^|
    Echo.  ^|      1 = Blue        9 = Light Blue             ^|
    Echo.  ^|      2 = Green       A = Light Green            ^|
    Echo.  ^|      3 = Aqua        B = Light Aqua             ^|
    Echo.  ^|      4 = Red         C = Light Red              ^|
    Echo.  ^|      5 = Purple      D = Light Purple           ^|
    Echo.  ^|      6 = Yellow      E = Light Yellow           ^|
    Echo.  ^|      7 = White       F = Bright White           ^|
    Echo.  ^|                                                 ^|
    Echo.  ^|  Example:                                       ^|
    Echo.  ^|                                                 ^|
    Echo.  ^|  0a  (black background with light green text)   ^|
    Echo.  ^|                                                 ^|
    Echo.  +-------------------------------------------------+
    Echo.
    
    Set colorspecs=
    Echo. Enter 2-character color code from the ranges 0-9 and/or A-F,
    Echo. or enter nothing to quit
    Set /P colorspecs=^>^>^>
    Set infoMessage=
    
    GoTo:eof
    
    :myValidateFunc
    
    REM check for entered characters that are not in the list of codes
    Set char1Found=False
    Set char2Found=False
    Set validEntries=True
    
    For /D %%a IN (A B C D E F 0 1 2 3 4 5 6 7 8 9) DO If %%a==%char1% Set char1Found=True
    
    For /D %%a IN (A B C D E F 0 1 2 3 4 5 6 7 8 9) DO If %%a==%char2% Set char2Found=True
    
    If %char1Found%==False Set validEntries=False
    If %char2Found%==False Set validEntries=False
    
    If %validEntries%==False Set char1=
    
    GoTo:eof
    
    SETLOCAL DISABLEDELAYEDEXPANSION
    Last edited by JerryDal; 11-20-14 at 12:51. Reason: echo args passed to getInputFunc changed to %*

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •