-1

i was trying to make a programme that changed colour but it just does not work. I press any button and it does not change and then just closes. I have no clue why it doesent work

@echo off
set letter2=0
:1
set color=%random%
if %color% LSS 10 goto next
goto 1
:next
set letter=%random%
if %random% LSS 6 goto 2
:2 
if %letter% == 0 goto A
if %letter% == 1 goto B
if %letter% == 2 goto C
if %letter% == 3 goto D
if %letter% == 4 goto E
if %letter% == 5 goto F
goto next
:a
set %letterr2% == a
goto final
:b
set %letterr2% == b
goto final
:c
set %letterr2% == c
goto final
:d
set %letterr2% == d
goto final
:e
set %letterr2% == e
goto final
:f
set %letterr2% == f
:final
set realcolor=%letter2%+%color%
cls
color %realcolor%
echo hey this color is %color%
pause>nul
goto 1

3 Answers3

5

There are a couple things going on here.

The biggest problem is that your set statements in :a through :f are completely wrong.

  • When you write a set statement, only use one = symbol.
  • When you say set %letter2%=c, you are not setting the value of letter2 to c, you are saying "set the value of what the value of letter2 is to c."
  • Remove the spaces from both sides of the = symbol; batch allows spaces to be part of variable names, so you've created a variable called %letterr2 % and set its value to c.
  • You made a typo and called the variable %letterr2% instead of %letter2%.

You don't need a + for string concatenation, just stick the two variables next to each other.

Your script is taking forever to run because %random% returns a number between 1 and 32768. The odds of it being under 10 are miniscule. The odds of it being under 6 are even smaller. When you want a random number between 1 and n, use the code set /a number=%random% %% n.

Ultimately, your code is going to look something like this:

@echo off
set letter2=0
:1
set /a color=%random%%%10
set /a letter=%random%%%6

if %letter% == 0 goto A
if %letter% == 1 goto B
if %letter% == 2 goto C
if %letter% == 3 goto D
if %letter% == 4 goto E
if %letter% == 5 goto F

:a
set letter2=a
goto final
:b
set letter2=b
goto final
:c
set letter2=c
goto final
:d
set letter2=d
goto final
:e
set letter2=e
goto final
:f
set letter2=f
:final
set realcolor=%letter2%%color%
cls
color %realcolor%
echo hey this color is %color%
pause>nul
goto 1
SomethingDark
  • 13,229
  • 5
  • 50
  • 55
3

If you want to know why your code does not work, see the extensive explanation of @SomethingDark.

However, if you want to see a simpler way to do the same thing, then you may review this code:

@echo off
setlocal EnableDelayedExpansion

set letters=abcdef

:loop
set /A color=%random% %% 10
set /A letter=%random% %% 6

set letter2=!letters:~%letter%,1!

set realcolor=%letter2%%color%
cls
color %realcolor%
echo hey this color is %realcolor%
pause>nul
goto loop

They keypoint in this code is the use of Delayed Expansion, so I suggest you to search for such a term in this site where are tons of related answers, like this one, or this one, or this one, or this one, or...

Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108
1
@echo off

:loop
    call :getRandom
    cls
    color %randomColor%
    echo This color is %randomColor%
    pause >nul
goto :loop

:getRandom
    set "randomColor="
    :getRandomLoop
        set /a "color=%random% %% 15"
        set "if=if %color%=="
        set "then=set color="
            %if%10 %then%A
            %if%11 %then%B
            %if%12 %then%C
            %if%13 %then%D
            %if%14 %then%E
            %if%15 %then%F
        set "randomColor=%randomColor%%color%"
        if "%randomColor:~1,1%"=="" goto :getRandomLoop
goto :EOF
Sam Denty
  • 3,693
  • 3
  • 30
  • 43
  • 2
    Instead of giving new code, you should actually answer the question and explain why the existing code doesn't work. – SomethingDark Apr 11 '17 at 11:55
  • 1
    I don't see the point in showing this type of code. It uses a syntax that nobody uses and that certainly will confuse a Batch file beginner, like the OP... **`:(`** – Aacini Apr 11 '17 at 15:20
  • You should explain the reason for your downvote in my answer (other than a _revenge downvote_), like I did it here... **`:/`** – Aacini Apr 11 '17 at 16:44