所以我有一个名为Userdetails.dll的文件 它包含以下内容。
ABC XYZ
DEF ZYX
GHI YXZ
假设每行左侧的字符串是用户的名称,右侧的字符串是密码。
现在,如何创建允许用户输入其用户名的批处理文件。然后,该脚本将从列表中检查用户名是否有效。如果它有效,它允许用户输入密码,如果没有则退出。如果用户输入的密码与.txt文件中提到的密码匹配,则打印“登录”(即Echo登录)。如果没有,它会回显“无效密码”并将用户循环到输入密码阶段。
我试过在论坛上搜索这个,但我找不到任何直接链接。但是所有的链接都让我在代码中使用了For / f和Findstr。 我需要知道如何使用/ f和findstr来提供所需的结果。 for / f %% A in('findstr“User”userdetails.txt')d o set usrname = %% A
但是它返回用户名而不是用户的相关密码。 因此,我想知道如何使用该命令从通过在文件中搜索输入的用户名返回密码。 注意。我不是程序员(专业)。因此,我请你解释你一行一行写的代码。非常感谢。
答案 0 :(得分:0)
默认情况下,for /f
使用空格和制表符将输入行标记为分隔符,并仅读取行中的第一个标记。
由于输入行包含空格,因此仅检索行中的第一个元素。您需要禁用分隔符以检索for
可替换参数
for /f "delims=" %%a in ('
findstr /l /c:"%whatever%" userdetails.txt
`) do set "userdetails=%%a"
或者您可以指示您要检索行中的两个第一个令牌,每个令牌都存储在单独的for
可替换参数中
for /f "tokens=1,2" %%a in ('
findstr /l /c:"%whatever%" userdetails.txt
`) do (
set "username=%%a"
set "password=%%b"
)
答案 1 :(得分:0)
答案是出于怜悯。不要让人们为你编写代码,这是获得标记的好方法。
@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