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

Jeff Zeitlin
  • 9,773
  • 2
  • 21
  • 33
hetot he
  • 13
  • 2
  • What exactly are you asking, and what's not working with this? – Jeff Zeitlin Sep 07 '17 at 18:19
  • Possible duplicate of [How to use if - else structure in a batch file?](https://stackoverflow.com/questions/11081735/how-to-use-if-else-structure-in-a-batch-file) – Alejandro Sep 07 '17 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%`) – MC ND Sep 07 '17 at 18:34

1 Answers1

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
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • Simpler: initialize `set "color=abcde"` and then use `color !color:~%num%,1!` instead of all those `if`'s (with Delayed Expansion Enabled). – Aacini Sep 07 '17 at 23:45
  • That was my first thought too, but wanted to stay with the simple code of OP @Aacini –  Sep 08 '17 at 06:10