Prophet
Prophet

Reputation: 3

If/then/else statements in Batch

I am trying to make a script that displays random numbers (resembling the matrix) for a few seconds then it changes red and displays "error" I have yet to find the proper syntax for an If/then/else statement. Any help would be appreciated

@echo off
color 02
:tricks 
set /a counter +=1
echo %random%%random%%random%%random%%random%%random%
if %counter% > %random% * %random% * %random% * %random% * %random% * %random% + 15000 goto error
goto tricks
:error
color 04
:err
echo %error%
goto err

I know that the

echo %random%%random%%random%%random%%random%%random%

works, but the rest after that is what I'm having trouble with. i use notepad++ to write and i have no way to compile the program so I cant see what errors I've made, I only know that it won't run. -Much Obliged-

Upvotes: 0

Views: 372

Answers (2)

Potato Head
Potato Head

Reputation: 143

Here's a slightly dorkish matrix style batch file that I modified to have an error screen. This will only echo 0,1's

:: Matrix style batch file
@ECHO OFF
@BREAK OFF
:: Number of times to run matrix before error
SET Rotations=75
:: Number of times to flash error
SET ErrorTimes=5
:Matrix
COLOR 02
SET /A A=%random%*2/32767
SET /A B=%random%*2/32767
SET /A C=%random%*2/32767
SET /A D=%random%*2/32767
SET /A E=%random%*2/32767
SET /A F=%random%*2/32767
SET /A G=%random%*2/32767
SET /A H=%random%*2/32767
SET /A I=%random%*2/32767
SET /A J=%random%*2/32767
SET /A K=%random%*2/32767
SET /A L=%random%*2/32767
SET /A M=%random%*2/32767
SET /A N=%random%*2/32767
SET /A O=%random%*2/32767
SET /A P=%random%*2/32767
SET /A Q=%random%*2/32767
SET /A R=%random%*2/32767
SET /A S=%random%*2/32767
SET /A T=%random%*2/32767
SET /A U=%random%*2/32767
SET /A V=%random%*2/32767
SET /A W=%random%*2/32767
SET /A X=%random%*2/32767
SET /A Y=%random%*2/32767
SET /A Z=%random%*2/32767
ECHO %A%%D%%C%%B%%B%%F%%A%%E%%F%%G%%H%%C%%Z%%X%%Y%%F%%E%%R%%B%%M%%Z%%P%%K%%U%%I%%G%%S%%A%%D%%L%%O%%P%%W%%T%%Y%%B%%N%%F%%U%%S%%P%%S%%T%%U%%G%%H%%S%%L%%R%%S%%Q%%W%%E%%R%%T%%V%%B%%N%%O%%U%%I%%L%%P%%S%%F%%E%%C%%X%%Z%%B%%H%%R%%U%%L%%O%%M%%E%%E%%S%
SET /A Matrix+=1
IF %Matrix% LEQ %Rotations% GOTO :Matrix
SET Matrix=0
GOTO :Error

:Error
CLS
COLOR 04
ECHO oooooooooooo ooooooooo.   ooooooooo.     .oooooo.   ooooooooo.
ECHO `888'     `8 `888   `Y88. `888   `Y88.  d8P'  `Y8b  `888   `Y88.
ECHO  888          888   .d88'  888   .d88' 888      888  888   .d88'
ECHO  888oooo8     888ooo88P'   888ooo88P'  888      888  888ooo88P'
ECHO  888    "     888`88b.     888`88b.    888      888  888`88b.
ECHO  888       o  888  `88b.   888  `88b.  `88b    d88'  888  `88b.
ECHO o888ooooood8 o888o  o888o o888o  o888o  `Y8bood8P'  o888o  o888o
TIMEOUT /T 1 /NOBREAK>NUL
CLS
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
TIMEOUT /T 1 /NOBREAK>NUL
CLS
SET /A Error+=1
IF %Error% LEQ %ErrorTimes% GOTO :Error
SET Error=0
GOTO :Matrix

Upvotes: 0

Jason Faulkner
Jason Faulkner

Reputation: 6558

You have several issues.

When doing comparisons in an IF statement, you must use GTR with the /I flag (see if /? for full details):

if /i %counter% GTR [somevalue]

In the above, "somevalue" must be a value, not a calculation. For example, you may want to do something like this:

SET /A Target=%RANDOM% + 1500
if /i %counter% GTR %Target% goto error

However, this creates a different comparison each time, so you may want to work on the logic a bit.

Additionally, when you print "error" (which prints infinitely by the way) just use echo error instead of echo %error% (which isn't defined and will just say "ECHO is off" over and over again).


If you are looking for an alternate way to simulate the matrix, then the following script would do it. This is completely outside of your problem, but I just wanted to give a working sample which could do what you want.

@ECHO OFF
SETLOCAL EnableExtensions EnableDelayedExpansion

COLOR 02

REM Generate 1500 random lines.
FOR /L %%A IN (1,1,1500) DO (
    SET Giberish=
    REM Generate 79 random numbers to ensure the desired length.
    FOR /L %%X IN (1,1,79) DO SET Giberish=!Giberish!!RANDOM!

    REM Trim to 79 chars.
    REM Since the default console length is 80, 79 will fill the entire line without a break.
    SET Giberish=!Giberish:~1,79!

    ECHO !Giberish!
)

REM Print linebreaks to move the numbers off the screen.
FOR /L %%A IN (1,1,50) DO ECHO.

COLOR 04
ECHO Error

ENDLOCAL

Upvotes: 1

Related Questions