Read a variable from a text file

2019-07-19 19:19发布

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.

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.

2条回答
啃猪蹄的小仙女
2楼-- · 2019-07-19 20:07

Read the file into a variable like this:

set /p content=<"C:\path\to\Q47.txt"
查看更多
Luminary・发光体
3楼-- · 2019-07-19 20:12

You might try this:

@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      
查看更多
登录 后发表回答