user2263904
user2263904

Reputation: 1

Batch file complicated if sequence returns errors.

I am trying to have users login and when they do it sets their power or rank. The ranks are Admin User and Guest. I need to do the code below for every command in the program. It returns ) was not expected at this time. Any idea why? For this command all the users should be able to access it but later I will need to set certain things for each group.

if %inputCommand%==/help (
  if %power%==Admin (
goto helpInfo
  ) else (
    if %power%==User (
  goto helpInfo
) else (
  if %power%==Guest (

      ) else (
        goto powerReadFailed
      )
    )
  )
) else (
goto readInputCommandTwo
    )

Upvotes: 0

Views: 69

Answers (2)

Magoo
Magoo

Reputation: 80113

Why so complicated?

how about

for %%a in (admin user guest) do if /i "%power"=="%%a" (
 for %%b in (help somethingelse whatever) do if /i "%inputcommand"=="%%b" (
 goto %%a%%b
 )
 goto %%ainvalidcommand
)
:powerreadfailed

you can then set up labels

adminhelp userhelp guesthelp
adminsomethingelse usersomethingelse guetsomethingelse
...

It's perfectly legitimate to write

:adminhelp
:userhelp
echo admin and user get the same help

and adding a new user-class or command isn't hard...

( IF /i means make a case-insensitive comparison)

Upvotes: 0

Endoro
Endoro

Reputation: 37579

Try this:

if "%inputCommand%"=="/help" (
  if "%power%"=="Admin" (
goto helpInfo
  ) else (
    if "%power%"=="User" (
  goto helpInfo
) else (
  if "%power%"=="Guest" (
        rem
      ) else (
        goto powerReadFailed
      )
    )
  )
) else (
goto readInputCommandTwo
    )

Upvotes: 1

Related Questions