批处理 - 比较变量

时间:2016-10-12 23:03:02

标签: batch-file variables

我想知道如何比较变量

我的代码:

:pass
set /p pass= "Password: "
set read = type Users\%user%\Password\password.txt
if pass = read goto continue
if not pass = read goto passwrong

我试图让它读取password.txt文件并将其与用户为登录系统输入的密码进行比较。如果密码正确(与password.txt匹配),它将转到continue脚本(运行实际程序),否则会转到密码错误

1 个答案:

答案 0 :(得分:1)

如果密码是password.txt的第一个非空行,请尝试此操作:

set /p "pass=Password: "
set /p read=<"%USERPROFILE%\Desktop\PROGRAM NAME\Users\test\Password\password.txt"
if "%pass%"=="%read%" (goto :continue) else (goto :passwrong)

如果密码是password.txt文件的最后一个非空行:

set /p "pass=Password: "
for /f usebackdelims^=^ eol^= %%i in (
        "%USERPROFILE%\Desktop\PROGRAM NAME\Users\test\Password\password.txt"
) do set "read=%%i"
if "%pass%"=="%read%" (goto :continue) else (goto :passwrong)