BATCH变量替换失败

时间:2016-03-05 01:28:34

标签: batch-file

我的批处理代码(专门用于简单地更改颜色)失败,因为替换代码失败。它应该替换的文本就像输入一样。

echo Colors:
echo Black, blue, green, red, purple, yellow, white, gray, light blue, light   green, light aqua, light red, light purple, light yellow, and bright white.
set /p COLOR=What color for the background? 
set DASH=%COLOR: =-%
set COLORSETBACK=%DASH:black=0%
set COLORSETBACK=%DASH:blue=1%
set COLORSETBACK=%DASH:green=2%
set COLORSETBACK=%DASH:aqua=3%
set COLORSETBACK=%DASH:red=4%
set COLORSETBACK=%DASH:purple=5%
set COLORSETBACK=%DASH:yellow=6%
set COLORSETBACK=%DASH:white=7%
set COLORSETBACK=%DASH:gray=8%
set COLORSETBACK=%DASH:grey=8%
set COLORSETBACK=%DASH:light-blue=9%
set COLORSETBACK=%DASH:light-green=A%
set COLORSETBACK=%DASH:light-aqua=B%
set COLORSETBACK=%DASH:light-red=C%
set COLORSETBACK=%DASH:light-purple=D%
set COLORSETBACK=%DASH:light-yellow=E%
set COLORSETBACK=%DASH:bright-white=F%
cls
echo Colors:
echo Black, blue, green, red, purple, yellow, white, gray, light blue, light   green, light aqua, light red, light purple, light yellow, and bright white.
set /p COLOR=What color for the text? 
set DASH=%COLOR: =-%
set COLORSETTEXT=%DASH:black=0%
set COLORSETTEXT=%DASH:blue=1%
set COLORSETTEXT=%DASH:green=2%
set COLORSETTEXT=%DASH:aqua=3%
set COLORSETTEXT=%DASH:red=4%
set COLORSETTEXT=%DASH:purple=5%
set COLORSETTEXT=%DASH:yellow=6%
set COLORSETBACK=%DASH:white=7%
set COLORSETTEXT=%DASH:gray=8%
set COLORSETTEXT=%DASH:grey=8%
set COLORSETTEXT=%DASH:light-blue=9%
set COLORSETBACK=%DASH:light-green=A%
set COLORSETTEXT=%DASH:light-aqua=B%
set COLORSETTEXT=%DASH:light-red=C%
set COLORSETTEXT=%DASH:light-purple=D%
set COLORSETTEXT=%DASH:light-yellow=E%
set COLORSETTEXT=%DASH:bright-white=F%
color %COLORSETBACK%%COLORSETTEXT%

设置DASH =%COLOR:= - %是要改变"浅红色"浅红色"它的工作原理。 然而,当我到达设置COLORSETTEXT =%DASH:black = 0%时,代码无法用0替换黑色。我在互联网的不同部分尝试了不同的问题而没有运气。如果我为背景键入红色,为文本键入黑色,则输出将为" redblack"我需要它是" 40"

2 个答案:

答案 0 :(得分:6)

每次读取颜色时,您的代码都会执行所有行。例如,如果给定颜色为black,则第一行正确地将“黑色”更改为“0”,但在此之后执行其余行,因此最后一行行执行set COLORSETBACK=%DASH:bright-white=F%并且DASH的原始值 更改(因为它包含“明亮 - 白色“),最后再次分配black。这意味着您的代码仅在最后一个颜色读取时起作用:“亮白色”。

您的代码应该if命令每个值,因此在完成正确的替换后,其余的替换将被省略。然而,这会产生非常冗长和丑陋的代码......

“以相同方式处理一系列值”的正确方法是通过for命令改变下标的值并结合array concept

@echo off
setlocal EnableDelayedExpansion

rem Define the "color" array used in the replacement:
set hex=0123456789ABCDEF
set i=0
for %%a in (Black, blue, green, aqua, red, purple, yellow, white, gray,
            light-blue, light-green, light-aqua, light-red, light-purple, light-yellow, bright-white) do (
   for %%i in (!i!) do set "color[%%a]=!hex:~%%i,1!"
   set /A i+=1
)

echo Colors:
echo Black, blue, green, red, purple, yellow, white, gray, light blue, light green, light aqua, light red, light purple, light yellow, and bright white.
set /p COLOR=What color for the background? 
set DASH=%COLOR: =-%
set COLORSETBACK=!color[%DASH%]!

cls
echo Colors:
echo Black, blue, green, red, purple, yellow, white, gray, light blue, light green, light aqua, light red, light purple, light yellow, and bright white.
set /p COLOR=What color for the text? 
set DASH=%COLOR: =-%
set COLORSETTEXT=!color[%DASH%]!

color %COLORSETBACK%%COLORSETTEXT%

您可以在Arrays, linked lists and other data structures in cmd.exe (batch) script

的批处理文件中阅读有关阵列管理的更多说明

PS - 您在 TEXT 部分的这些行中也有一些错误:

set COLORSETBACK=%DASH:white=7%
set COLORSETBACK=%DASH:light-green=A%

编辑:我添加了原始代码中缺少的“aqua”颜色。我也很好地利用了这个编辑来添加一个更小的解决方案:

@echo off
setlocal EnableDelayedExpansion

rem Define the "color" array used in the replacement:
set "colors=Black, blue, green, aqua, red, purple, yellow, white, gray, light-blue, light-green, light-aqua, light-red, light-purple, light-yellow, bright-white"
set "hex=0123456789ABCDEF"
set "i=0"
for %%a in (%colors%) do (
   for %%i in (!i!) do set "color[%%a]=!hex:~%%i,1!"
   set /A i+=1
)

for %%c in (background text) do (
   cls
   echo Colors:
   echo %colors:-= %
   set /p COLOR=What color for the %%c?
   set "%%c=!color[%COLOR: =-%]!"
)

color %background%%text%

答案 1 :(得分:2)

我修改了你的代码 - 它能做你需要的吗?

通过取消注释两个REM语句,它们可以向您显示文本的修改方式。

@echo off
set "foreground="
set "background="

cls
echo Color choices:
echo Black, blue, green, red, purple, yellow, white, gray, light blue, light   green, light aqua, light red, light purple, light yellow, and bright white.
echo(
:loop
if not defined foreground set type=text
if not defined background set type=background
set "color="
set /p "COLOR=What color for the %type%? : "
set COLOR=%COLOR: =-%
rem echo %color%
set COLOR=%COLOR:light-blue=9%
set COLOR=%COLOR:light-green=A%
set COLOR=%COLOR:light-aqua=B%
set COLOR=%COLOR:light-red=C%
set COLOR=%COLOR:light-purple=D%
set COLOR=%COLOR:light-yellow=E%
set COLOR=%COLOR:bright-white=F%
set COLOR=%COLOR:black=0%
set COLOR=%COLOR:blue=1%
set COLOR=%COLOR:green=2%
set COLOR=%COLOR:aqua=3%
set COLOR=%COLOR:red=4%
set COLOR=%COLOR:purple=5%
set COLOR=%COLOR:yellow=6%
set COLOR=%COLOR:white=7%
set COLOR=%COLOR:gray=8%
set COLOR=%COLOR:grey=8%
rem echo %color%
if not defined background set background=%color%&goto :loop
if not defined foreground set foreground=%color%
echo(
echo color %background%%foreground%
pause