批处理如何将文本文档转换为变量

Batch how to convert text document to variable

现在我正在处理一个具有简单登录和注册功能的批处理文件。虽然我可以通过为他们的用户名和密码创建一个 .txt 文件来注册用户,但我无法弄清楚如何将 .txt 文件的内容转换为一个变量,然后我可以测试该变量以登录用户。这就是我现在所拥有的。我的问题在 :sign .

@echo off
title Login
:a
cls
mode con: lines=10 cols=25
echo _________________________
echo     Select Your Choice
echo      1) Login
echo      2) Register
echo _________________________
set /p begi=:
if '%begi%'=='2' goto reg
if '%begi%'=='1' goto sign

:reg
cls
set u=
set /p u=Enter Your Username:
pause>nul
cls
set p=
set /p p=Enter your Password:
pause>nul
@echo %u% > user.txt
@echo %p% > pass.txt
pause>nul
goto a

:sign
cls
exit

REM write to the file:
echo Username>user.txt
REM read from the file (first line only):
set /p "un="<user.txt
echo %un%

顺便说一句: 您的行 @echo %u% > user.txt 将变量加上 space 写入文件。这个 space 将成为新变量的一部分(并且在使用新变量时会遇到麻烦),如果你读入这个文件。为了避免它,写 @echo %u%>user.txt