Board logo

标题: [文本处理] [分享]批处理判断输入密码错误超过3次之后退出 [打印本页]

作者: Batcher    时间: 2021-12-11 21:43     标题: [分享]批处理判断输入密码错误超过3次之后退出

【问题描述】

谁帮忙看看怎么输入错误3次后才退出,现在是一次直接退出
作者: Batcher    时间: 2021-12-11 21:43

【解决方案】
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. set "ScriptPassword=bbs.bathome.net"
  4. set "InputCount=0"
  5. set "InputMax=3"
  6. :GetPassword
  7. set "InputPassword="
  8. set /p "InputPassword=Please input password: "
  9. set /a InputCount+=1
  10. if "!InputPassword!" neq "%ScriptPassword%" (
  11.     if !InputCount! lss %InputMax% (
  12.         goto :GetPassword
  13.     ) else (
  14.         goto :PasswordWrong
  15.     )
  16. ) else (
  17.     goto :PasswordCorrect
  18. )
  19. :PasswordWrong
  20. echo You tried too many times.
  21. goto :End
  22. :PasswordCorrect
  23. echo Hello World
  24. goto :End
  25. :End
  26. pause
  27. goto :eof
复制代码

作者: Batcher    时间: 2021-12-11 22:46

输入密码显示星号,且限制输错密码的次数:
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. set "ScriptPassword=BatHome"
  4. set "InputCount=0"
  5. set "InputMax=3"
  6. 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"
  7. for %%a in (%str%) do (
  8.     set /a n+=1
  9.     set ".!n!=%%a"
  10. )
  11. :PasswordLength
  12. set StrLen=0
  13. for /f "skip=1 delims=:" %%a in ('^(echo "%ScriptPassword%"^&echo,^)^|findstr /o ".*"') do (
  14.     set /a StrLen=%%a-5
  15. )
  16. :GetPassword
  17. cls
  18. set /p =请输入密码:<nul
  19. if defined pwds (
  20.     set /p =%pwds%<nul
  21. )
  22. if "%m%" equ "%StrLen%" (
  23.     echo,
  24.     goto :VerifyPassword
  25. )
  26. choice /n /cs /c %str: =%
  27. set "pwds=%pwds%*"
  28. set "pwd=%pwd%!.%errorlevel%!"
  29. set /a m+=1
  30. goto :GetPassword
  31. :VerifyPassword
  32. set /a InputCount+=1
  33. if "!pwd!" neq "%ScriptPassword%" (
  34.     if !InputCount! lss %InputMax% (
  35.         echo 密码错误
  36.         pause
  37.         set "m=0"
  38.         set "pwds="
  39.         goto :GetPassword
  40.     ) else (
  41.         goto :PasswordWrong
  42.     )
  43. ) else (
  44.     goto :PasswordCorrect
  45. )
  46. :PasswordWrong
  47. echo 输错密码次错过多
  48. goto :End
  49. :PasswordCorrect
  50. echo 密码正确
  51. goto :End
  52. :End
  53. pause
  54. goto :eof
复制代码
扩展阅读:批处理输入密码但显示星号
http://bbs.bathome.net/thread-2130-1-1.html




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2