So I have a file named as Userdetails.dll It contains the following content.
ABC XYZ
DEF ZYX
GHI YXZ
Let's say the strings on the left side of each line are the names of users and the ones on the right are passwords.
Now, how do I create a batch file wherein the User is allowed to input his User name. The script then checks if the user name is valid from the list. If it is valid, it allows the user to input the password, if not then it exits. If the password inputted by the user matches with that mentioned in the .txt file, then it prints "Logged in" (ie Echo Logged in). If not it echoes "Invalid Password" and loops the user to Entering the Password Stage.
I have tried searching the the forum for this, but I did not find any direct link. However all of the links led me to the use of For /f and Findstr in my code. I need to know how to use for /f and findstr to provide the desired result. for /f %%A in ('findstr "User" userdetails.txt') d o set usrname=%%A
However it returns the username and not the relevant password of the user. Hence I would like to know how to use the command to return password from the username inputted by searching it in the file. Note. I am not a programmer(by profession). Hence I request you to explain me the code you have written line by line. Thanks a lot.
An answer out of mercy. Don't ask people to just write code for you, good way to get flagged.
@rem Hide the commands being submitted, standard to make things not look ugly.
@echo off
rem Completely optional and useless color change.
color f0
rem Use delayed expansion for 'safer' variable handling.
rem Delayed expansion means, variables are translated to their values as they are needed, rather than having a set in stone value.
rem This means special symbols will be treated as strings instead of as a command, and a variable in a block of code can have it's value changed and read in the same block.
setlocal enableDelayedExpansion
rem Set path to the file that holds the usernames and password.
set "dataFile=C:\Users\Aresc\Desktop\userPass.txt"
:main
rem Call username method.
call :requestUsername
rem Call password method.
call :requestPassword
rem Pause,
pause
rem Then close.
exit
:requestUsername
rem Wipe the screen
cls
rem Wipe variables.
set "strUsername="
set "strCurrentUserPass="
rem Prompt for username via user input, storing into strUsername.
set /p "strUsername=Input Username:"
rem If the username is blank aka they just pressed {enter}, ask again.
if "!strUsername!" equ "" goto :requestUsername
rem Loop our datafile, line by line, grabbing the first and second word,
rem the first word is stored temp as %%G, second temp as %%H.
for /f "tokens=1,2" %%G in (!dataFile!) do (
rem Check if the first word [username in dataFile] matches input username.
if "!strUsername!" equ "%%G" (
rem If it does, store the second word [the password in out dataFile] as a variable for later.
set "strCurrentUserPass=%%H"
rem Return to the call in ':main'.
rem Note, a 'goto :eof' tells cmd to return to the last 'call' command location.
rem In this case, the last call was from ':main', when i ran 'call :requestUsername'
goto :eof
)
)
rem If that above `for /f` loop found no matches with the `if` check, the input username wasnt valid.
exit
:requestPassword
rem Wipe variables
set "strPassword="
rem Prompt for password via user input, storing into strPassword.
set /p "strPassword=Input Password for user '!strUsername!':"
rem If the username is blank aka they just pressed {enter}, ask again.
if "!strUsername!" equ "" goto :requestPassword
rem Check if the user input matches the variable we stored from the `for /f` and `if` check.
if "!strPassword!" equ "!strCurrentUserPass!" (
rem If it did match, do stuff, in this case echo to the screen,
echo Logged in.
rem then return to the call in `:main`.
goto :eof
) else (
rem If it did not match, tell them
echo Invalid Password.
rem Write a blank line, useless but it's an attempt to make a batch menu look organized.
echo:
rem Ask them again.
goto :requestPassword
)
rem If there is some error with the `if` check above, exit. [Good habit.]
exit