需要批量帮助。 (出乎意料的错误

时间:2015-01-22 22:34:03

标签: batch-file

所以我正在编写批处理并提出(设置第一个变量后出现意外错误。我想要一个批处理文件,只需设置颜色即可。(我将其命名为edmond) 这是我的代码

    @echo off
SETLOCAL
title Edmond
goto :Check
:Prompt
set /p action=What shall I do for you master? 
:Check
if not defined action goto :Prompt
echo %action%
pause
if /i %%action == color (
    echo stuff
    :cl
    set /p BC=What Should the background color be?
    set /p FC=and the foreground color?     
    if %BC%==Black set BC1=0
    if %BC%==Blue set BC1=1
    if %BC%==Green set BC1=2
    if %BC%==Aqua set BC1=3
    if %BC%==Red set BC1=4
    if %BC%==Purple set BC1=5
    if %BC%==Yellow set BC1=6
    if %BC%==White set BC1=7
    if %BC%==Gray set BC1=8
    if %BC%==LBlue set BC1=9
    if %BC%==LGreen set BC1=a 
    if %BC%==LAqua set BC1=b 
    if %BC%==LRed set BC1=c
    if %BC%==LPurple set BC1=d
    if %BC%==LYellow set BC1=e
    if %BC%==LWhite ( 
    set BC1=f
    ) else (
        echo I'm sorry, I didn't exactly understand that.
        echo By any chance could you say it again?
        goto cl
    )

    if %FC%==Black set FC1=0
    if %FC%==Blue set FC1=1
    if %FC%==Green set FC1=2
    if %FC%==Aqua set FC1=3
    if %FC%==Red set FC1=4
    if %FC%==Purple set FC1=5
    if %FC%==Yellow set FC1=6
    if %FC%==White set FC1=7
    if %FC%==Gray set FC1=8
    if %FC%==LBlue set FC1=9
    if %FC%==LGreen set FC1=a
    if %FC%==LAqua set FC1=b
    if %FC%==LRed set FC1=c
    if %FC%==LPurple set FC1=d
    if %FC%==LYellow set FC1=e
    if %FC%==LWhite ( 
    set FC1=f
    ) else (
        echo I'm sorry, I didn't exactly understand that.
    )
    echo Applying changes.
    ping localhost -n 2 >nul
    color %FC%%BC%
) else (
    echo I'm sorry, I didn't exactly understand that.
)
pause
exit
在设置/ p动作之后<=>我应该为你做什么?它说(出乎意料然后关闭(我得到了(在关闭之前打印屏幕出乎意料)可以有人帮忙吗?谢谢(提前)

1 个答案:

答案 0 :(得分:2)

if /i %%action == color (

应该是

if /i %action% == color (

或更好

if /i "%action%"=="color" (

因为您有不受控制的输入,可能包含空格或其他分隔符。

你即将出现的问题很多。

您不能在(块语句 - 一系列带括号的语句)中使用标签

你需要

setlocal enabledelayedexpansion

然后使用!var!代替%var%使用块语句来访问在块中更改或建立的任何普通环境变量的值。


非常截断的修订结构:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
:Prompt
set /p action=What shall I do for you master? 
:Check
if not defined action goto :Prompt
echo %action%
if /i "%action%"=="color" GOTO setcolor
echo I'm sorry, I didn't exactly understand that.
goto Prompt

:setcolor
echo stuff
:cl
set /p BC=What Should the background color be?
set /p FC=and the foreground color?
:: This forces BC1 & FC1 to be undefined
SET "BC1="
SET "FC1="
if /i "%BC%"=="Black" set BC1=0
if /i "%BC%"=="Blue" set BC1=1
:: Your job to fill in the rest
IF NOT DEFINED BC1 (
   echo I'm sorry, I didn't exactly understand that.
   echo By any chance could you say it again?
   goto cl
)

if /i "%FC%"=="Yellow" set FC1=6
:: Your job to fill in the rest
IF NOT DEFINED BC1 (
   echo I'm sorry, I didn't exactly understand that.
   rem I suppose you really want to re-enter at this point.
   rem note that you need to use REM within a block, not ::-style comments.
   goto cl
)
echo Applying changes.
ping localhost -n 2 >NUL
:: Best to use FC1 and BC1 here, else you'll try to execute "color YellowBlack"
:: And it's likely you have them reversed.
color %FC1%%BC1%
GOTO :EOF