-1

I've been trying to get this code to work...

I've tried re doing the code... to no avail also, the code below is a bit big...

:ST
if exist password.txt goto login
if not exist password.txt goto signup
:login
set /p user=<username.txt
set /p pass=<password.txt
set /p answer=Username: 
if %answer% = %user% goto p1
if NOT %answer% = %user% goto errlog111
exit
:p1
set /p answer=Password: 
if %answer%=%pass% goto 
if NOT %answer% = %pass% goto errlog111
echo success
pause
exit
:signup
title Username needed for account creation! / Newex / Unregistered
set /p answer=Username: 
echo %answer% > username.txt
cls
set /p answer=Password: 
echo %answer% > password.txt
cls
echo Account created!
goto login
exit
:errorlog111
echo Username / Password incorrect!
pause
goto login

So, what I want is for it to check the contents of password.txt, (or username.txt) for what the user inputs, and if the user inputs the right thing, it goes on to the next thing. However, if they get the password, or username wrong then I want it to say "Username / Password wrong!" but for some reason when I enter the username, the batch file just closes! Please help!

mycool
  • 3
  • 1
  • 1
    `If "%answer%"=="%user%"`, `If Not "%answer%"=="%user%`, `If "%answer%"=="%pass%"` and `If Not "%answer%"=="%pass%"`, would seem to better follow the help information shown for `If /?`, with the doublequotes added for safety, _(protects any special characters in the input entries)_. – Compo Mar 31 '19 at 09:48
  • 1
    The batch file does not crash. Windows command processor `cmd.exe` processing your batch file line by line encounters a serious syntax error, outputs an appropriate error message and exits batch file processing as it can be seen on [debugging your batch file](https://stackoverflow.com/a/42448601/3074564). I recommend also reading my answer on [How to stop Windows command interpreter from quitting batch file execution on an incorrect user input?](https://stackoverflow.com/a/49834019/3074564) to make your batch file safe against intentional or by mistake wrong user input. – Mofi Mar 31 '19 at 10:28
  • Please consider reading the help from file for a command before you ask any further questions. As pointed out by comments and answers, this was a very basic syntax problem that can be seen plain as day in the help file. – Squashman Mar 31 '19 at 14:01
  • Just a note to mention the typo in my comment above, it should read, `If "%answer%"=="%user%"`, **`If Not "%answer%"=="%user%"`**, `If "%answer%"=="%pass%"` and `If Not "%answer%"=="%pass%"`, would seem to better follow the help information shown for `If /?`, with the doublequotes added for safety, _(protects any special characters in the input entries)_. – Compo Apr 01 '19 at 12:17
  • Thank you all for your help! It works now. And I didn't realize I could type if /? in the command prompt, lol. I am not very great at batch also... – mycool Apr 01 '19 at 17:30

1 Answers1

0
if %answer% = %user% goto p1

The correct syntax is to use == or EQU.

Better syntax is

if "%answer%" == "%user%" goto p1
  • the quotes protect against some illegal inputs and where the arguments contain certain characters like , ; and would be advised for any comparison between strings.

Note that if /i ... makes the comparison case-insensitive (for future reference)

Also - a hard-to-see error

echo %answer% > username.txt

will include the space which follows the value of answer into the file

> username.txt echo %answer%

will not include this (presumably) unwanted space.

Magoo
  • 77,302
  • 8
  • 62
  • 84