Reputation: 55
I recently started trying to make a "login" in bacth by using some variables and if statements. What happened is when I run this code in CMD and I try to log in without having made a username or password, it should bring me to the label "FAIL" but instead it gives me the error "The syntax of the command is incorrect." While this doesn't tell me what lines I messed up on, I think its finding a problem with the IF/ELSE statements in the "LOGIN" label. Have a look for me, see if you can figure out what I'm doing wrong.
@ECHO off
SET /P CHOICE=Do you have a username/password? Y/N
IF %CHOICE%==y (
GOTO LOGIN
)
IF %CHOICE%==Y (
GOTO LOGIN
)
IF %CHOICE%==n (
GOTO SIGNUP
)
IF %CHOICE%==N (
GOTO SIGNUP
)
:SIGNUP
CLS
ECHO Please choose your username/password below
SET /P USER1=Username:
SET /P PASS1=Password:
CLS
ECHO Your log-in credentials should be set now
SET /P CHOICE=Do you want to log-in? Y/N
IF %CHOICE%==y (
GOTO LOGIN
)
IF %CHOICE%==Y (
GOTO LOGIN
)
IF %CHOICE%==n (
GOTO END
)
IF %CHOICE%==N (
GOTO END
)
:LOGIN
CLS
ECHO Please type in your username/password below
SET /P USER2=Username:
SET /P PASS2=Password:
IF EXIST %USER1% (
IF %USER2%==%USER1% (
GOTO SUCCESS
) ELSE (
GOTO FAIL
)
) ELSE (
GOTO FAIL
)
IF EXIST %PASS1% (
IF %PASS2%==%PASS1% (
GOTO SUCCESS
) ELSE (
GOTO FAIL
)
) ELSE (
GOTO FAIL
)
:SUCCESS
CLS
ECHO You have been successfully logged in
ECHO.
ECHO Press any key to continue
PAUSE>NUL
GOTO END
:FAIL
CLS
ECHO The username/password you entered was incorrect or does not exist
ECHO.
SET /P RETRY=Try again? Y/N
IF %RETRY%==y (
GOTO LOGIN
)
IF %RETRY%==Y (
GOTO LOGIN
)
IF %RETRY%==n (
GOTO END
)
IF %RETRY%==N (
GOTO END
)
:END
CLS
ECHO Press any key to exit
PAUSE>NUL
Upvotes: 4
Views: 14520
Reputation: 5056
Here's the trick I use, though it's kind of odd:
IF "%USER1%"=="%USER2%" (REM Do stuff...)
For input bob
and bob
, it resolves to this:
IF "bob"=="bob" (REM Do stuff...)
For input bob
and joe
, it resolves to this:
IF "bob"=="joe" (REM Do stuff...)
For input
and joe
, it resolves to this:
IF ""=="joe" (REM Do stuff...)
You could also test for empty input like this:
IF "%USER1%"=="" (Echo Please enter something.)
The IF
statement was complaining when given nothing because it was getting IF ==joe
, which is a syntax error.
Aditionally, I noticed you were doing IF EXIST %Pass1%
, which isn't correct. Per the documentation in if /?
:
EXIST filename Specifies a true condition if the specified filename exists.
You (appear to be) using this to check if the variable is defined. You do this (as I said before) with IF "%VAR1%"=="%VAR2%"
.
As a minor note (though I didn't correct it below), you can do if /I "%str1%"=="y"
to do case-insensitive checking (IE it matches both y
and Y
) so that you don't need redundant code.
Here's your entire script formatted to behave like this:
@ECHO off
SET /P CHOICE=Do you have a username/password? Y/N
IF "%CHOICE%"=="y" (
GOTO LOGIN
)
IF "%CHOICE%"=="Y" (
GOTO LOGIN
)
IF "%CHOICE%"=="n" (
GOTO SIGNUP
)
IF "%CHOICE%"=="N" (
GOTO SIGNUP
)
:SIGNUP
CLS
ECHO Please choose your username/password below
SET /P USER1=Username:
SET /P PASS1=Password:
CLS
ECHO Your log-in credentials should be set now
SET /P CHOICE=Do you want to log-in? Y/N
IF "%CHOICE%"=="y" (
GOTO LOGIN
)
IF "%CHOICE%"=="Y" (
GOTO LOGIN
)
IF "%CHOICE%"=="n" (
GOTO END
)
IF "%CHOICE%"=="N" (
GOTO END
)
:LOGIN
CLS
ECHO Please type in your username/password below
SET /P USER2=Username:
SET /P PASS2=Password:
IF "%USER2%"=="%USER1%" (
GOTO SUCCESS
) ELSE (
GOTO FAIL
)
IF "%PASS2%"=="%PASS1%" (
GOTO SUCCESS
) ELSE (
GOTO FAIL
)
:SUCCESS
CLS
ECHO You have been successfully logged in
ECHO.
ECHO Press any key to continue
PAUSE>NUL
GOTO END
:FAIL
CLS
ECHO The username/password you entered was incorrect or does not exist
ECHO.
SET /P RETRY=Try again? Y/N
IF "%RETRY%"=="y" (
GOTO LOGIN
)
IF "%RETRY%"=="Y" (
GOTO LOGIN
)
IF "%RETRY%==n" (
GOTO END
)
IF "%RETRY%"=="N" (
GOTO END
)
:END
CLS
ECHO Press any key to exit
PAUSE>NUL
Upvotes: 3