找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 16261|回复: 2

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

[复制链接]
发表于 2021-12-11 21:43:32 | 显示全部楼层 |阅读模式
【问题描述】

谁帮忙看看怎么输入错误3次后才退出,现在是一次直接退出
 楼主| 发表于 2021-12-11 21:43:54 | 显示全部楼层
【解决方案】
  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
复制代码
 楼主| 发表于 2021-12-11 22:46:02 | 显示全部楼层
输入密码显示星号,且限制输错密码的次数:
  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
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-20 16:32 , Processed in 0.008959 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表