我有这段代码。它基本上是一个随机密码生成器。我输入我想要的密码长度,比如“10”,然后我得到一个由 10 个字母、数字和符号组成的随机密码,并将其保存在一个 txt 文件中。当我创建第二个密码时,就会出现问题。如果我再次想要一个长度为“10”的密码,但它只给我一个长度为 3 或 4 的密码。这取决于情况。有人知道我哪里做错了吗?任何帮助都非常感谢。
@Echo Off
mode con: cols=70 lines=25
:start
color 8a
echo ----------------------------------------------------------------------
echo -------- Password Generator -------
echo ----------------------------------------------------------------------
echo.
echo.
Echo Hello %username%
set /P lengthnumberuser="What length do you want your password to be?:
cls
Setlocal EnableDelayedExpansion
Set _RNDLength=%lengthnumberuser%
Set _Alphanumeric=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789%%$$@@**
Set _Str=%_Alphanumeric%987654321
:_LenLoop
IF NOT "%_Str:~18%"=="" SET _Str=%_Str:~9%& SET /A _Len+=9& GOTO :_LenLoop
SET _tmp=%_Str:~9,1%
SET /A _Len=_Len+_tmp
Set _count=0
SET _RndAlphaNum=
:_loop
Set /a _count+=1
SET _RND=%Random%
Set /A _RND=_RND%%%_Len%
SET _RndAlphaNum=!_RndAlphaNum!!_Alphanumeric:~%_RND%,1!
If !_count! lss %_RNDLength% goto _loop
echo.
echo.
echo.
cls
Echo Password is: !_RndAlphaNum!
set /p _detail=Please Record a description for this password:
echo.
echo %date%---%_detail%----!_RndAlphaNum! >> RandPass.txt
echo Password Saved with Details to RandPass.txt for future reference.
echo.
echo.
set /p _newpass= Would you like to create another password ?[y/n]:
cls
If /i "%_newpass%"== "y" GOTO :start else :end
:end
答案1
我不喜欢过长的变量名,但这是可行的:
@Echo Off
mode con: cols=70 lines=25
:start
color 8a
echo ----------------------------------------------------------------------
echo -------- Password Generator -------
echo ----------------------------------------------------------------------
echo.
echo.
Echo Hello %username%
set /P len="What length do you want your password to be?:
cls
Setlocal EnableDelayedExpansion
Set "Chars=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789$@*"
Set CharsCnt=65
Set "Pass="
For /L %%c in (1,1,%Len%) do (
Set /a Pnt=!Random! %% CharsCnt
Call Set "Pass=!Pass!%%Chars:~!Pnt!,1%%"
)
cls
Echo Password is: %Pass%
set /p _detail=Please Record a description for this password:
echo.
echo %date%---%_detail%----%Pass% >> RandPass.txt
echo Password Saved with Details to RandPass.txt for future reference.
echo.
echo.
set /p _newpass= Would you like to create another password ?[y/n]:
cls
If /i "%_newpass%"== "y" GOTO :start
:end
顺便说一句:输出几行空行然后输出 cls 对我来说没有意义。