-1

I'm trying to make a game using batch files. At the beginning of the game, the player can set the colors of the command prompt window. I want to save the color they choose (a 2-digit hex code) as a parameter or variable, and put it into another file which saves the color code. I tried to do this (see below) but it doesn't seem to work. Does anyone have any answers?

Here's my code (I'm somewhat new to coding, so I'm sorry if it doesn't look great):

Batch File 1:

set colorvalue=F0
set %6=%colorvalue%
pause
exit

Batch File 2:

echo The color should change once you continue.
pause
color %6
pause
echo Did it work?
pause
exit

Along with this, I tried using the contents of a .txt file as the color value.

Batch File 1:

@echo off
echo F0>value.txt
pause
exit

The above code should make value.txt (which is in the same location as the batch file) say F0, and this works.

Batch File 2:

echo off
color value.txt
pause
exit

I'm assuming I'm doing something wrong at "color value.txt." The output is simply the color help message.

COLOR [attr]

  attr        Specifies color attribute of console output

Color attributes are specified by TWO hex digits -- the first
corresponds to the background; the second the foreground.  Each digit
can be any of the following values:

    0 = Black       8 = Gray
    1 = Blue        9 = Light Blue
    2 = Green       A = Light Green
    3 = Aqua        B = Light Aqua
    4 = Red         C = Light Red
    5 = Purple      D = Light Purple
    6 = Yellow      E = Light Yellow
    7 = White       F = Bright White

If no argument is given, this command restores the color to what it was
when CMD.EXE started.  This value either comes from the current console
window, the /T command line switch or from the DefaultColor registry
value.

The COLOR command sets ERRORLEVEL to 1 if an attempt is made to execute
the COLOR command with a foreground and background color that are the
same.

Example: "COLOR fc" produces light red on bright white

I tried all of this, but the color doesn't change. I'm willing to try anything, any help is appreciated! :D

P.S. I've tried simply setting %6 to the hex code (F0 as an example) but that didn't work either.

IgroSome
  • 3
  • 6
  • See https://stackoverflow.com/questions/26551/how-can-i-pass-arguments-to-a-batch-file?rq=1, `%6` does not make sense unless you are passing 6 arguments to the file which you only use 1 so the rest would be redundant. – Nico Nekoru Jun 08 '20 at 19:21
  • Literally, `set colors=F0 && color $colors%` – Nico Nekoru Jun 08 '20 at 19:23
  • These two programs allow you to query for current colour https://winsourcecode.blogspot.com/2019/12/getconsolecolourexe-prints-current.html (use `for /f` to put into a variable) and to set the colour of a line of text https://winsourcecode.blogspot.com/2019/12/colourtext-changes-colour-of-text-to-be.html –  Jun 08 '20 at 20:06
  • You might be interested in [this](https://stackoverflow.com/a/55694870/2152082) – Stephan Jun 08 '20 at 20:10
  • @NekoMusume, [this](https://stackoverflow.com/questions/62269681/is-there-a-way-to-change-the-color-of-a-batch-file-using-a-variable-or-parameter#comment110129135_62269681) would require delayed expansion… – aschipfl Jun 09 '20 at 08:04
  • You seem to confuse [command line arguments](https://ss64.com/nt/syntax-args.html) with [environment variables](https://ss64.com/nt/syntax-variables.html). You cannot set argument references like `%6`. You could however do `> "colour.txt" echo %COLOUR%` to save the value in variable `%COLOUR% `to a file and `< "colour.txt" set /P COLOUR=""` to load it… – aschipfl Jun 09 '20 at 08:09

2 Answers2

0

Short answer

set colors=F0 
color %colors%

Explanation for your code

Batch File 1:

set colorvalue=F0
set %6=%colorvalue%
pause
exit

Batch File 2:

echo The color should change once you continue.
pause
color %6
pause
echo Did it work?
pause
exit

%number variables cannot be defined in batch files since they are special variables that hold arguments passed to the file. For more info see How can I pass arguments to a batch file?. %6 would be the sixth argument passed to the batch file and would not make sense unless you are passing 6 arguments to the file which you only use 1 so the rest would be redundant. Even if you could define variables like this, you would have to call the variable like this:

%%6%

Which doesn't look as good as:

%6%

anyways which you would define with

set "6=F0"

Second Attempt

Batch File 1:

@echo off
echo F0>value.txt
pause
exit

Batch File 2:

echo off
color value.txt
pause
exit

This is not how STDIN works. See Read stdin stream in a batch file. To read data from a file, you have to use a for loop like this:

for %%a in (value.txt) do (set 6=%%a)

Where again, %6% would get set to the line being read in the text file. See for /? in cmd for more help with for.

Third Attempt

I've tried simply setting %6 to the hex code (F0 as an example) but that didn't work either.

Batch-files don't do hex codes. Simple as that.

Nico Nekoru
  • 2,840
  • 2
  • 17
  • 38
0

You seem to confuse command line arguments with environment variables. You cannot set argument references like %6.

You could however do:

> "colour.txt" echo %COLOUR%

to save the value in variable %COLOUR%to a file and:

< "colour.txt" set /P COLOUR=""

to load it.

Here is all this in one example script (consult the explanatory rem remarks in the code):

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constants here:
set "_COLFILE=%~dpn0_colours.txt"

rem /* Attempt to read colour value from file; suppress error message when
rem    file is not found; if an error occurs, prompt for colour value: */
(< "%_COLFILE%" set /P COLOUR="") 2> nul || call :COL_ASK

:COL_SET
    rem // Ensure that colour value consists of exactly two characters:
    set "COLOUR=00%COLOUR%"
    set "COLOUR=%COLOUR:~,2%"
    rem /* Try to convert hex colour value into an integer number; when it
    rem    fails, prompt for colour value (again): */
    2> nul set /A "0x%COLOUR%" || (call :COL_ASK & goto :COL_SET)
    rem /* Try to set specified colours; when foreground and background
    rem    colours are the same, prompt for colour value (again): */
    color %COLOUR% || (call :COL_ASK & goto :COL_SET)
    rem // Finally store colour value into file:
    > "%_COLFILE%" echo/%COLOUR%

endlocal
exit /B


:COL_ASK
    rem // Sub-routine that prompts for colour value:
    echo What colours do you want to set?
    rem // Display excerpt of help message of `color` command:
    color /? | findstr /B /C:"    "
    rem // Clear variable to avoid keeping previous value upon empty input:
    set "COLOUR="
    rem // Actually prompt for colour value:
    set /P COLOUR="Value: "
    exit /B

This is not absolutely fail-safe against weird arbitrary user input but a good starting point though.

aschipfl
  • 33,626
  • 12
  • 54
  • 99