0

im making a game in batch because its simple, and i was trying to write a else statment, but ive figured out i can only use 1 else in every class, so only 1 input will work but i need multipul to work

cls
color 02
ECHO Welcome to your hacking console, you can type main to get back to the main menu

set /p input=Command:

if %input%==main (
goto main
) else (
goto error
)
if %input%==tracert (
goto tracert
) else (
goto error
)
if %input%==IPlookup (
goto IPlookup
) else (
goto error
)
Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156

2 Answers2

2

You don't need an else - you just want to do something if none of the other if statements do something:

cls
color 02
ECHO Welcome to your hacking console, you can type main to get back to the main menu
set /p input=Command:
if %input%==main goto main
if %input%==tracert goto tracert
if %input%==IPlookup goto IPlookup
goto error
Jerry Jeremiah
  • 9,045
  • 2
  • 23
  • 32
  • 1
    I would recomend adding `"` between your variables. saves the chance of crashes. read more on this: http://stackoverflow.com/questions/22418771/batch-set-p-crashes-when-no-input?rq=1 –  Aug 18 '14 at 05:06
0

Whenever you have only one command after an if statement, you can just post it afterwards. I know Jerry had a great answer, but I just felt like adding more detail.

If I needed to perform multiple tasks, the ( would be very helpful. here's an example:

if %x%==1 (
:: do some stuff
:: do more stuff
:: do even more stuff
)

But if I only need to do some stuff, I would just put it in one line.

if %x%==1 :: do some stuff

Since in your script, you are jumping to another label, once you hit that if statement, your away from that part of the script.. Here's what I mean:

if %x%==1 goto label_01
echo this won't get displayed unless %x% isn't equal to "1"

:label_01
echo anything after the "if" line wont get displayed, since we are here.

And since batch scripts are read by cmd.exe from left to right, and top to bottom, we can stack up those if statements.

if %x%==1 goto label_01
if %x%==2 goto label_02
if %x%==3 goto label_03
:: we now know that %x% is not equal to 1, 2, or three.  so now we can take advantage of that and post some script.
:: do some stuff

Since your script seems to be based off of a menu system, I recommend changing it up a bit:

cls
color 02
ECHO Welcome to your hacking console!
echo.
echo Press 1 to     go back to the main menu
echo Press 2 to     Tracerout
echo Press 3 to     Lookup IP address
choice /c 123 >nul
set input=%errorlevel%
if %input%==1 goto main
if %input%==2 goto tracert
if %input%==3 goto IPlookup

This method saves us the need to goto error. It also saves time, because now instead of typing out a whole word, you just type a number. If you type an unsupported number, nothing will happen.

Of course, this method won't work if your on XP.

BTW, a hacking machine? I remember scripting a couple of these. Except mine had the options of a DOS attack, and a browser password extractor :)

  • I always compile my batch files, and my compiler screws up the errorlevel like in a .cmd file. That's why I set the variable %input% to errorlevel, because that very same if statement will reset the errorlevel. Either way, this will still work. –  Aug 18 '14 at 05:11