To solve your problem, you need to be aware of a number of issues.
It's regarded as unwise in any programming language to use a keyword as a variable-name.
ALL variables in batch are STRINGS. No exceptions.
set /a
allows pure-numeric strings to be used as if they were numeric values and perform calculations using those values. It also allows variable names alone to be used in calculations - without requiring the %
delimiters.
The result of a set /a
calculation is stored in suppressed-leading-0 decimal format.
In a set /a
, a leading 0
has special significance. If the value begins 0x
then the value is interpreted as HEX, otherwise of the value begins 0
, then the value is interpreted as OCTAL.
If a redirector (><|) is directly preceded in the source by any numeric character, then the redirection takes place of the nominated "stream" - 0=keyboard, 1=standard output, 2=standard error; others unused)
"quoting a string" causes cmd
to interpret the string as a single entity, even if it contains spaces or other separators. This is especially useful for absolute filenames (containing paths).
Tip: Use set "var=value"
for setting string values - this avoids problems caused by trailing spaces. Don't assign "
or a terminal backslash or Space. Build pathnames from the elements - counterintuitively, it is likely to make the process easier. If the syntax set var="value"
is used, then the quotes become part of the value assigned.
The color
syntax is color bf
where b
is the background colour and f
the foreground. These characters are hex (0..9,a..f;case-insensitive) therefore the instruction color %myhues%
must have myhues
set to a 2-character string, each character being hex.
So, set /a
will yield a leading-zero-suppressed decimal result, it's inapplicable for controlling color
settings.
Your code
echo 04 > filename
will actually record 04SpaceCRLF on the file (you'll see the filelength is 5
) because the space appears before the redirector.
So - you remove the space
echo 04> filename
... and it doesn't work, because 4>
redirects stream4 to the file.
You could use
echo 0^4> filename
Where the caret (^) "escapes" the special meaning of the 4
. But that's really awkward.
The normal method would be
> filename echo 04
which is fine, provided filename
contains no separators and there are no trailing spaces on the line.
So
> "filename" echo 04
is better; the space after the >
is optional (and extra typing)
So, you need to assign a string of two hex characters to myhues
as appropriate.
What might be useful is this:
:setbgred
set "myhues=4%myhues:~-1%"
to set the background colour to red(4
) and keep the current foreground.
There are oodles of examples of how to use substringing in batch on SO. As a last resort, you could even read the documentation (from the prompt: set /?
)