从文本文件读取变量 [英] Read a variable from a text file
问题描述
我正在尝试创建一个密码提示,该提示将用户输入与文本文件中的信息进行比较(密码保存在.txt文件中)。
I'm trying to create a password prompt which compares the user input to information in a text file (the password is saved in the .txt file).
我已经尝试使用通过命令提示符和此网站提供给我的信息,但是我无法使其正常工作,可能是因为我没有足够的经验,因为我不太熟悉高级批处理编码。
I've tried to work with information provided to me through the command prompt and and this website but I just can't get it to work, probably because i don't have sufficient experience as I'm rather new to advanced batch coding.
到目前为止,这是我想出的,文本文件的名称为Q47.txt,其中只有 hello一词,直到我可以使其工作:
This is what I've come up with so far, the name of the text file is Q47.txt and in it is just the word "hello" until I can get this to work:
@echo off
:a
cls
SetLocal EnableDelayedExpansion
set content=
for /F "delims=" %%i in (Q47.txt) do set content=!content! %%i
echo %content%
EndLocal
echo Enter password to continue:
set /p "VAR=>"
if "%VAR%" == "%content%" (
goto begin
)
echo Incorrect, please try again.
pause >nul
goto a
:begin
cls
echo Welcome
pause >nul
请您告诉我我哪里出问题了。
Please can you tell me where I've gone wrong.
我还想知道如何消除变量前的空格。
I'd also like to know how to eliminate the space before the variable.
推荐答案
您可以尝试以下操作:
@ECHO OFF &SETLOCAL
:a
set "content="
for /F "tokens=*" %%i in (Q47.txt) do set "content=%%i"
echo "%content%"
echo Enter password to continue:
set /p "VAR=>"
if "%VAR%" == "%content%" goto begin
echo Incorrect, please try again.
pause >nul
goto a
:begin
cls
echo Welcome
pause >nul
这篇关于从文本文件读取变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!