0
@echo off
mode 1000
goto block1


:block1
echo color
goto block2


:block2
pause
set /a num=%random% %%5
goto 0

:0
if num == 0 goto a
goto 1


:1
if num == 1 goto b
goto 2


:2
if num == 2 goto c
goto 3


:3
if num == 3 goto d
goto 4


:4
if num == 4 goto e
goto 5


:5 
if num == 5 goto f
goto 0

:a
color 0a
goto block2


:b
color 0b
goto block2


:c
color 0c
goto block2


:d
color 0d
goto block2


:e
color 0e
goto block2


:f
color 0f
goto block2

i want to make a color sign witch changes colors and i would like use simething like this . but i cant use else in batch and it would need something like that

3
  • What exactly are you asking, and what's not working with this? Commented Sep 7, 2017 at 18:19
  • Possible duplicate of How to use if - else structure in a batch file? Commented Sep 7, 2017 at 18:32
  • In batch files the syntax to refer to a variable name (num) differs from the syntax to obtain the variable content (%num%) Commented Sep 7, 2017 at 18:34

1 Answer 1

0
  • From batch view point nearly all the gotos are useless and only result in spaghetti code.
  • You are aware that a color stement will affect the whole screen?
  • %random% modulus 5 can only return 0..4

Your code shortened :

@echo off
mode 1000
echo color
:block2
pause
set /a num=%random% %%5
if %num% == 0 color 0a
if %num% == 1 color 0b
if %num% == 2 color 0c
if %num% == 3 color 0d
if %num% == 4 color 0e
goto block2
Sign up to request clarification or add additional context in comments.

2 Comments

Simpler: initialize set "color=abcde" and then use color !color:~%num%,1! instead of all those if's (with Delayed Expansion Enabled).
That was my first thought too, but wanted to stay with the simple code of OP @Aacini

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.