The original code, as per your link is this:
Setlocal EnableDelayedExpansion
Set _RNDLength=9
Set _Alphanumeric=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567899876543210
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 0!_RndAlphaNum! >> your-new-password.txt
I would first clean it up a bit for consistency and a little more readability and add @ECHO OFF
to hide the commands so it runs clean:
@ECHO OFF
Setlocal EnableDelayedExpansion
SET _RNDLength=9
SET _Alphanumeric=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567899876543210
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 !_RndAlphaNum!>> your-new-password.txt
BTW, putting @
in front of a line prevents the line from being echoed. Since to turn echoing off for the whole script requires a command that will be echoed, it is best to include this on that line so it itself will not be echoed. Hopefully that makes sense.
Now for the solution. You will need 2 more variables, one for the maximum number of passwords and one for tracking how many passwords have been created. The first one can be set when _RNDLength
is set, and the second one should be set with the first. This second one should be set to 1
because after the password is created, it will be incremented and checked. If, when checked, it is not less than the max (that is, equal to), then the script will exit. If we had set it to 0
, because of the style here, we would have 1001 passwords instead of 1000 passwords.
SET _PwdMax=1000
SET _PwdCount=1
Then after the echo command at the end, you should add a command to increment the second variable and an IF statement that checks if the number of passwords created is less than the max number of passwords wanted.
SET /a _PwdCount+=1
IF !_PwdCount! LSS %_PwdMax% goto _LenLoop
All together, your code should look like this:
@ECHO OFF
Setlocal EnableDelayedExpansion
SET _RNDLength=9
SET _PwdMax=1000
SET _PwdCount=1
SET _Alphanumeric=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567899876543210
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 !_RndAlphaNum! >> your-new-password.txt
SET /a _PwdCount+=1
IF !_PwdCount! LSS %_PwdMax% goto _LenLoop
0..999 | ForEach -Begin {$all = [Char[]]( [Char[]](0..255) -clike '[A-Za-z0-9]' )} -Process {-join(Get-Random -InputObject $all -Count 10)}