A .bat file that changes the background color according to the number that the user typed [duplicate]

0

The code below should change the background color after the user types a number from 0 to 7, but the program closes shortly after the user types the number and does not change the color. I'm using the win7 cmd.

 @ECHO OFF
 SET /p num=Digite um numero de zero a sete:

IF %num% == 0 (
color 0f
) ELSE (
IF %num% == 1 (
color 1f
) ELSE (
IF %num% == 2 (
color 2f
) ELSE (
IF %num% == 3 (
color 3f
) ELSE (
IF %num% == 4 (
color 4f
) ELSE (
IF %num% == 5 (
color 5f
) ELSE (
IF %num% == 6 (
color 6f
) ELSE (
IF %num% == 7 (
color 7f
) ELSE (
ECHO error.
)
)
pause
    
asked by anonymous 25.02.2017 / 15:22

2 answers

1

Use this code:

@ECHO OFF
SET /p num=Digite um numero de zero a sete:
IF %num% LEQ 7 (
    color %num%f
) ELSE (
    ECHO error.
)

pause
    
25.02.2017 / 17:13
0

BAT files do not handle parentheses very well, the statement the IF executes comes from the side of it: IF [condition] [instruction] and not: IF [condition]     [instruction]

logo:

@ECHO OFF
SET /p num=Digite um numero de zero a sete:

IF %num% == 0 color 0f
IF %num% == 1 color 1f
IF %num% == 2 color 2f
IF %num% == 3 color 3f
IF %num% == 4 color 4f
IF %num% == 5 color 5f
IF %num% == 6 color 6f
IF %num% == 7 color 7f
pause
    
26.02.2017 / 12:43