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!
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.
Related
I just started coding two days ago. (It's oddly addictive). And I started so that I could make a simulated "computer hacking" experience as a portion of an escape room that I am designing. Mainly, I have just been looking around and finding different ideas and putting them together in an aesthetically pleasing way. However, I want to make it crash resistant. So that when people are using it they don't get booted for just button mashing and pressing enter.
I got this to work in the :menu but not on the faux log in page.
Below is the code that I wrote that I want to be able to work on other pages:
:menu
cls
set "answer=Nothing^!"
set var=%var: =%
echo.
echo Menu
echo.
echo 1. Login
echo 2. Instructions
echo 3. Exit
echo.
set /p answer=Type the number of your option and press enter:
echo.
echo.
if /I "!answer!" == "exit" goto exit
echo You answered: %answer%
echo Which is not a valid entry.
echo Try again.
if /I %answer% == 1 goto :Login1
if /I %answer% == 2 goto :Instructions
if /I %answer% == 3 goto :Exit
if /I %answer% == 43153 goto :done
pause
goto :menu
Here is the code I cannot get to work.
:Login2
cls
set "un=Nothing^!"
set "pw=Nothing^!"
set var=%var: =%
echo.
echo Log On
echo.
if %counter% lss 5 echo Password incorrect, %counter% attempts left
echo.
echo.
set /p un=Enter your UserID:
set /p pw=Enter your Password:
if /I %un% == ID (
if /I %pw% == PASSWORD goto :loading1
)
if /I !un! == "exit" (
if !pw! == "exit" goto loading2
goto :loading2
If I type something random to input such as: asdf asdf
The menu doesn't crash. But the login2 page does. It crashes if it is username and password OR just username OR just password that have spaces. And if I just "enter" through the screen it does not auto-populate " Nothing^! " like the menu does.
I would paste the entirety of the file below, but because with the decorative liberties I took it won't fit well on this page. And I can't find a place to upload it. But any comments or suggestions would be greatly appreciated.
Again, thank you for any help!
if /I "!answer!" == "exit" goto exit
is correct
if /I %un% == ID (
is syntactically correct, provided un contains a simple string not containing spaces.
if un contains some spaces then the command is interpreted as
if /I some spaces == ID (
and the required syntax is
if [/i] string1 operator string2 dothis
An operator like == is also a string, so cmd sees spaces where it's expecting an operator, so it protests.
To allow spaces (and other separators) in a string, "quote the string" as you did with the first if above.
Oh - and it's particular about the quotes - they must exactly match - you can't quote one side and not the other.
This applies to string arguments only - where the arguments are numerics or resolve to a numeric, the quotes are not used. If they are used with numeric arguments, the arguments will be compared alphabetically so 98 is greater than 123 because 9 is greater than 1.
Always enquote variables in your IF expressions.
IF /i "%un%" == "ID" ...
IF /i "%pw%" == "PASSWORD" ...
or even better use delayed expansion here
IF /i "!un!" == "ID" ...
IF /i "!pw!" == "PASSWORD" ...
I have made a batch script that is just a basic login screen, however, I want to make it so it adds a code to itself AFTER you register (i.e it lets you skip registering) I want to add this code:
:alreadyregistered
goto login
However for it to work properly, I also need it to remember your username/password. This script stores this text in a text document, but i don't know how to make it compare the username/password you signed in to the ones in the document. (something like this: if %username% == "what is in the notepad" the goto logged in if else goto error)
It would also be cool if you could fix some errors I have. I want to make it so if there was an error making your account a "user account" (get system error 5) it takes you to the accounterror section. I tried using errorlevel but it does not work. (if ERRORLEVEL NEQ 0 goto accounterror) Thanks for all your help, as I'm a beginner.
Here is my code:
#echo off
color fc
rem register
:register
title Register
cls
set /p username="Please type a username: "
echo username = %username% > logindetails.txt
powershell -Command $pword = read-host "Please Enter a Password" -AsSecureString ; $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword) ; [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR) > .tmp.txt & set /p password=<.tmp.txt & del .tmp.txt
echo password = %password% >> logindetails.txt
powershell -Command $pword = read-host "Please Retype your Password" -AsSecureString ; $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword) ; [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR) > .tmp.txt & set /p confirmpassword=<.tmp.txt & del .tmp.txt
if %username% == %password% goto matcherror
if %confirmpassword% NEQ %password% goto passwordnotmatch
goto login1
rem if passwords don't match
:passwordnotmatch
title ERROR
cls
(
mshta javascript:alert^("Passwords do not match, try again"^);close^(^);
)
goto register
rem this is for credential matching
:matcherror
title ERROR
cls
(
mshta javascript:alert^("Credientials cannot match, try again"^);close^(^);
)
goto register
rem username for login
:login1
title Login
cls
set /p usernamelogin="Please type your username: "
if %usernamelogin% == %username% goto login2
if %usernamelogin% NEQ %username% goto loginerror
rem this is incorrect username/password, will go back to username entering
:loginerror
title ERROR
cls
(
mshta javascript:alert^("Incorrect username or password"^);close^(^);
)
goto login1
rem password for login
:login2
title Login
powershell -Command $pword = read-host "Please Enter your Password" -AsSecureString ; $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword) ; [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR) > .tmp.txt & set /p passwordlogin=<.tmp.txt & del .tmp.txt
if %passwordlogin% == %password% goto loggedin
if %passwordlogin% NEQ %password% goto loginerror
rem after login is complete
:loggedin
title Welcome %username%
cls
echo Hello %username%, what would you like to do?
echo 1) Make your account an actual user account
echo 2) Exit
set /p something=">"
if %something% == 1 goto work
if %something% == 2 exit
if not defined something (
(
mshta javascript:alert^("Please enter a valid number"^);close^(^);
)
goto loggedin
)
rem make a user account using credentials from this file
:work
net user %username% %password% /add
(
mshta javascript:alert^("Congratulations, your account %username% was created!"^);close^(^);
)
goto loggedin
rem error making account an actual user account
:accounterror
cls
(
mshta javascript:alert^("Sorry, %username%, your account was not created due to an error"^);close^(^);
)
goto loggedin
(something like this: if %username% == "what is in the notepad" the
goto logged in if else goto error
that would be:
set /p "EnteredName=Please enter Username: "
<file.txt set /p NameFromFile=
if %NameFromFile%==%EnteredName% (
echo they are the same
) else (
echo they are NOT the same
)
Please don't use username as variable name, because it's a systemvariable used by windows (like date and time). Changing it could lead to unexpected results.
I want to add this code:
appending code to the batchfile is easy: just append it:
(echo :alreadyregistered
echo goto login)>>%dpnx0
%~dpnx0 stands for Drive, Path, Name and eXtension of the currently running batchfile
So I am experimenting and trying to get better at using batch but I'm stuck on an issue, and I cant figure it out. The save/load allows the user to input a password and save it to the blah.blah, then later load it using the password. The problem is I want the program to recognize if the user has a password and if they dont, it will give them the option to exit or make a new password.
:passwordwant
cls
set /p "passwant=Enter your new password: "
(
echo %passwant%
)>save.sav
goto :passwordload
pause >nul
:passwordload
cls
echo Enter password to enter the programme
set /p "pass=>"
if %pass%== %passwant% goto :home
<save.sav(
set /p pass=
)
You can use if exist to determine if a file exists (or if not exist to determine if it doesn't).
if not exist save.sav (
echo Password not found
REM If you're using Windows XP or earlier, you need to use set /p instead of choice
choice /c:NQ /M "Press N to create a password. Press Q to quit."
if %errorlevel% equ 0 goto :passwordwant
if %errorlevel% equ 1 exit /b
)
The following code is for connecting to Oracle database through batch file by prompting for Username, password and database instance. What I want is to exit from cmd itself if any of the credentials is invalid, else it should proceed as normal.
#ECHO OFF
:START
set /p U="Enter Username(User ID):"
set /p P="Enter Password(Password#o1234abc):
:NEXT
set /p a="Do you want to display details of 1a?(y/n)"
ECHO "%a%"
if "%a%" == "y" sqlplus %U%/%P% #".\AB_1a.sql"
if "%a%" == "n" goto A
set /p id1= "Please enter ID for 1a(press 's' to SKIP):"
if "%id1%" == "s" goto A
sqlplus %U%/%P% #".\AB_.sql" %id1%
goto A
:End
Please suggest any solution.
You could do something like this:
#echo off
setlocal enableDelayedExpansion
set /p U="Enter Username(User ID):"
set /p P="Enter Password(Password#o1234abc):
rem Check creds!
if "!U!" == "JoeBloggs" if "!P!" == "Pa$$w0rd" goto :CREDSOK
exit >nul
:CREDSOK
set /p a="Do you want to display details of 1a?(y/n)"
if "%a%" == "y" sqlplus %U%/%P% #".\AB_1a.sql"
if "%a%" == "n" goto A
set /p id1= "Please enter ID for 1a(press 's' to SKIP):"
if "%id1%" == "s" goto A
sqlplus %U%/%P% #".\AB_.sql" %id1%
goto A
:End
If creds are correct it will skip the line to exit and run the rest, if either of the creds are wrong it will hit the exit line and terminate cmd.
Thanks to #jeb who suggested delayed expansion for special characters, see comment below.
Hope this helps.
Okay so heres what I'm trying to do. I'm trying to make a batch file with a user input for a login. I would like to have the ability to create a new account. I have an idea, I just don't know how to go around implementing it. I have the user input his desired username and password and have it save to a text document like so:
#echo off
set /p user=Enter your desired username:
set /p pass=Enter your desired password:
echo %user% >> log.txt
echo %pass% >> log.txt
That works fine for me, but now what I would like to do is call up those two lines so that when they entered the correct username and password it will take them to their menu.
I know for a fact the the call won't work well with this. Is there any way to do what I'm trying to do?
P.S. I am aware that the txt file is not secure. I have ways around that.
You may do this to recover the saved values:
(
set /P savedUser=
set /P savedPass=
) < log.txt
This way:
if "%user%" == "%savedUser%" if "%pass%" == "%savedPass%" goto accessGranted
Another way to save the values is this:
echo set savedUser=%user%> log.bat
echo set savedPass=%pass%>> log.bat
and to recover the saved values:
call log
I'm decently proud of this :) This is pretty much a complete overhaul of your method.
This has two different files: login.bat which handles the login and login2.bat which handles the registration.
login.bat:
#echo off
choice /c:RL /m "Choose an option: Register (R) or Login (L).:
if errorlevel 2 goto login
if errorlevel 1 goto register
:register
start /wait C:\[path]\login2.bat
cls
goto login
:in
cls
echo Welcome %u%
echo.
echo Bla Bla Bla or start "a program"
pause
exit
:login
set /p u=Username
set /p p=Password
and login2.bat:
#echo off
:a
set /p a="Choose a Username"
set /p b="Choose a Password"
echo.
choice /m "Are you sure you would like your Username to be %a% and your Password to be %b%?"
if errorlevel 2 goto a
set q="if %%u%% equ %a% if %%p%% equ %b% goto in"
for /f "tokens=*" %%I in (%q%) do set m=%%I
echo %m% >>C:\[path]\login.bat
exit
The reason why :in, the label which handles what happens after you login successfully has to be in the middle instead of at the end of login.bat is so that the username/password combinations could be appended to the :login label. Obviously you could add other embellishments and change the name of the files, but this is my basic design.
Hope this helps!
This is what I do
#echo off
:: some code here
set /p username=please create a username:
set /p password=please create a password:
echo %username%>username.txt
echo %password%>password.txt
This will create a text file for both your username and password
I hope this helped. :)