How can I make my batch script add code to itself - batch-file

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

Related

The batch file keeps crashing when the user enters text

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.

User Input Batch File crashes # echo var

I am writing a basic batch script and I need to acquire user input.
The script appears to crash when I call the user input vars in an echo command.
(This is probably something very basic but I can't see the error...)
#echo off
echo ********************************
echo SHUTDOWN/RESTART
echo ********************************
echo.
set /p answer="Restart (R) or Shutdown(S)? "
set /p time="How soon ? "
echo you have selected %answer% in %time% seconds.
if "%answer%"=="" goto error
if "%time%"=="" goto error
if "%answer%"=="R" (
goto restart
) else (
goto shutdown
)
echo.
echo.
:restart
FOR /L %%AA IN (1,1,%1) DO (
echo Restarting in %%AA ...
)
The FOR command requires a single alphabetic character for its controlled variable. %AAA is invalid.

Exit from cmd on error in connection to Oracle database

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.

Need Assistance in creating a password/username system with batch

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. :)

log in batch program

i have been formulating and doing a batch program that enables the user to enter his password,just like that, and i seem to arrive at nothingness, can anybody show me how to create a batch program that enables the user to enter his password?and run a specific program when log in is successfull, by the way, i am using windows xp sp2
set /p "PASSWORD=Enter your password: "
I use the quotes so I can show the trailing space in the batch file. Otherwise it's not necessary; any trailing spaces will be echoed into the output.
set /? shows a number of ways of manipulating variables in cmd.exe
Here's something that works for me...
#echo off
:loginPASS
set /p password=Password:
if %password% == qwerty goto loginsuccessful
if %password% == %password% goto loginfailed
:loginfailed
echo Login failed, password was incorrect!
pause
cls
goto login
:loginsuccessful
echo LOGIN SUCCESSFUL!
cd <location of file (dont include its name) example: C: /Users/YOURUSERNAME/desktop>
<type in file name here>
if you want to make a username input too, then delete the loginPASS section make a new one called loginUSER and type in this:
:loginUSER
set /p username=Username
cls
if %username% == Admin goto AdminLogin
if %username% == Bob goto BobLogin
if %username% == %username% goto NoUsername
make a new section called NoUsername
:NoUsername
echo User not found, check your username again.
pause
cls
goto loginUSER
this will tell the user if they entered the wrong username.
after that make a new section for 1 of the users and name appropriately.
in this case you will name it AdminLogin.
After that repeat loginPASS and make the password for the user.
The whole code should look something like this:
#echo off
:AdminLogin
set /p password=Password:
if %password% == 158468 goto loginsuccessful
if %password% == %password% goto loginfailed
:loginfailedADMIN
echo Login failed, password was incorrect!
pause
cls
goto AdminLogin
:loginsuccessfulADMIN
echo LOGIN SUCCESSFUL!
cd <location of file (dont include its name) example: C: /Users/YOURUSERNAME/desktop>
<type in file name here>
pause
exit
You will want to make seperate login sections for each user.
Hope this helps!
P.S There might be other ways but this is what i use.

Resources