What's wrong with this batch code?

March 18, 2011 at 13:13:11
Specs: Windows Vista
Hi all,

I'm meddling with batch files and came across a problem. When I try to run this script to login to the program and enter the password, even when it's showing me what the password is, it gives me the "login failed" message. Here is the code:

::@echo off
:startup
pause
echo Welcome to JSFAI! (beta)
echo Please wait while JSFAI initiates.
ping localhost -n 2 >> null
cls
echo Welcome to JSFAI! (beta)
echo Please wait while JSFAI initiates..
ping localhost -n 2 >> null
cls
echo Welcome to JSFAI! (beta)
echo Please wait while JSFAI initiates...
ping localhost -n 2 >> null
cls
echo Welcome to JSFAI, an AI created by Joshua Fryer.
set /p currentuser=Are you the existing user? (Y/N)
if /i {%currentuser%}=={N} (
	goto register
) else (goto login)

:register
echo Please register.
set /p password=What do you want as your password?
echo Password:Password: %password% > profiles.txt
echo You are now a registered user!

::(This is the problem section)
:login
set _inputfile=C:\Users\Chris.BCCL\Desktop\profiles.txt
For /F "tokens=1,2* delims=:" %%A IN (%_InputFile%) DO Set _Data%%A=%%C
set _
set /p userpass=Enter the password:
if {%DataPassword%}=={jj} (
echo Login successful!
) else (echo Login Failed!)
pause
::(/problem section)

:shutdown
echo Shutting down...
ping localhost -n 3 >> null
del null
goto eof

Yes, it's really messy, i'm just trying to get it to work first.Any help would be appreciated.

Thanks.


See More: Whats wrong with this batch code?

Report •

#1
March 18, 2011 at 21:33:05
There are a few things I would suggest changing:

[1] You create a textfile called "null" to discard text when you can just use "nul" which you won't need to delete because it is the actual null device.

[2] Validating "set /p" should be set up to avoid problems, consider it someone enters "}=={} echo haha!&pause& if {" at your "y/n" prompt.

[3] The way you are using "for /f" %datapassword% will always contain the very last entry.

[4] "echo Password:Password: %password% > profiles.txt"
This will leave a space before and after %password% in profiles.txt

>> %inputfile% echo Password:Password:%password%

Would fix the spaces but you may also be left with the variable trying to execute.


Here is my interpretation... And you though it was already messy!

@echo off

set _inputfile=C:\Users\Chris.BCCL\Desktop\profiles.txt


:startup
pause
echo Welcome to JSFAI! (beta)
echo Please wait while JSFAI initiates.
ping localhost -n 2 >> nul
cls
echo Welcome to JSFAI! (beta)
echo Please wait while JSFAI initiates..
ping localhost -n 2 >> nul
cls
echo Welcome to JSFAI! (beta)
echo Please wait while JSFAI initiates...
ping localhost -n 2 >> nul
cls
echo Welcome to JSFAI, an AI created by Joshua Fryer.

:usrchk
set currentuser=
set /p currentuser=Are you the existing user? (Y/N)
if not defined currentuser goto usrchk
if /i "%currentuser:"=%"=="N" 	goto register
if /i "%currentuser:"=%"=="Y" goto login
goto usrchk

:register
echo Please register.

set uname=
set /p uname=What do you want as your Username? (" not allowed)
if not defined uname goto register
if "%uname:"=%"=="" goto register

:regpass
set password=
set /p password=What do you want as your password? (" not allowed)
if not defined password goto regpass
if "%password:"=%"=="" goto regpass

>> %_inputfile% echo "%uname:"=%"	"%password:"=%"
echo You are now a registered user!



:login
cls
set uname=
set /p uname=What is your Username?
if not defined uname goto login
if "%uname:"=%"=="" goto login
set userpass=
set /p userpass=What is your user password?
if not defined userpass goto login
if "%userpass:"=%"=="" goto login


set success=false
For /F "tokens=1,2 delims=	" %%A IN (%_InputFile%) DO (
   if "%%~A%%~B"=="%uname:"=%%userpass:"=%" set success=true
)
if %success%==false (
    Echo LOG IN FAILURE!!!!!
    goto usrchk
)

cls
echo You are logged in as "%uname:"=%"
pause

:shutdown
echo Shutting down...
ping localhost -n 3 >> nul
goto :eof


Report •

#2
March 19, 2011 at 05:52:37
Wow, thanks!
This code works exactly as I want it to. You're a lifesaver.
(This question probably won't be the last, though) :)

Report •

#3
March 20, 2011 at 15:17:50
[1] You create a textfile called "null" to discard text when you can just use "nul" which you won't need to delete because it is the actual null device.

And lets have a look to your script :P

Line 5: ping localhost -n 2 >> nul

Happy is ONE who says I am an OttoMAN.


Report •

Related Solutions

#4
March 20, 2011 at 15:45:08
@MUs

It makes no difference if it's an append or overwrite stream, the important thing is that it's going to nul not an actual file like null will.

I don't normally append, that part was already there and I only changed what I needed to.


Report •

Ask Question