标题: [文本处理] [分享]批处理判断输入密码错误超过3次之后退出 [打印本页]
作者: Batcher 时间: 2021-12-11 21:43 标题: [分享]批处理判断输入密码错误超过3次之后退出
【问题描述】
谁帮忙看看怎么输入错误3次后才退出,现在是一次直接退出
作者: Batcher 时间: 2021-12-11 21:43
【解决方案】- @echo off
- setlocal enabledelayedexpansion
- set "ScriptPassword=bbs.bathome.net"
- set "InputCount=0"
- set "InputMax=3"
-
- :GetPassword
- set "InputPassword="
- set /p "InputPassword=Please input password: "
- set /a InputCount+=1
- if "!InputPassword!" neq "%ScriptPassword%" (
- if !InputCount! lss %InputMax% (
- goto :GetPassword
- ) else (
- goto :PasswordWrong
- )
- ) else (
- goto :PasswordCorrect
- )
-
- :PasswordWrong
- echo You tried too many times.
- goto :End
-
- :PasswordCorrect
- echo Hello World
- goto :End
-
- :End
- pause
- goto :eof
复制代码
作者: Batcher 时间: 2021-12-11 22:46
输入密码显示星号,且限制输错密码的次数:- @echo off
- setlocal enabledelayedexpansion
- set "ScriptPassword=BatHome"
- set "InputCount=0"
- set "InputMax=3"
- set "str=0 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z"
- for %%a in (%str%) do (
- set /a n+=1
- set ".!n!=%%a"
- )
-
- :PasswordLength
- set StrLen=0
- for /f "skip=1 delims=:" %%a in ('^(echo "%ScriptPassword%"^&echo,^)^|findstr /o ".*"') do (
- set /a StrLen=%%a-5
- )
-
- :GetPassword
- cls
- set /p =请输入密码:<nul
- if defined pwds (
- set /p =%pwds%<nul
- )
- if "%m%" equ "%StrLen%" (
- echo,
- goto :VerifyPassword
- )
- choice /n /cs /c %str: =%
- set "pwds=%pwds%*"
- set "pwd=%pwd%!.%errorlevel%!"
- set /a m+=1
- goto :GetPassword
-
- :VerifyPassword
- set /a InputCount+=1
- if "!pwd!" neq "%ScriptPassword%" (
- if !InputCount! lss %InputMax% (
- echo 密码错误
- pause
- set "m=0"
- set "pwds="
- goto :GetPassword
- ) else (
- goto :PasswordWrong
- )
- ) else (
- goto :PasswordCorrect
- )
-
- :PasswordWrong
- echo 输错密码次错过多
- goto :End
-
- :PasswordCorrect
- echo 密码正确
- goto :End
-
- :End
- pause
- goto :eof
复制代码
扩展阅读:批处理输入密码但显示星号
http://bbs.bathome.net/thread-2130-1-1.html
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |