0

I would like to make a random letter/number generator thingy in batch. I want it to generate 1000 times each time it's executed, but how do i do so it generates the new random password on a new line.

So for example if i open it 1 time, it generates 1000 lines, with a password on each line beginning with 0, then 9 random letters, then it's done?

https://ghostbin.co/paste/57uw8 (Link to code)

8
  • 1
    Please edit the question to include the code (and format it properly). Do not link to random off site links.
    – DavidPostill
    Jan 10, 2020 at 20:05
  • 2
    Why do you say 5000 random letters in your title, then ask for 1000 lines of 9 letters? Jan 10, 2020 at 20:23
  • Your code seems to work it generates 9 random letters/numbers but it only runs once. You have to somehow embbed it in a for /l loop to make it run 1000 / 5000 times: for /l %%a in (1,1,1000) do ..... Jan 11, 2020 at 1:57
  • 2
    In PowerShell, you can make it a one-liner: 0..999 | ForEach -Begin {$all = [Char[]]( [Char[]](0..255) -clike '[A-Za-z0-9]' )} -Process {-join(Get-Random -InputObject $all -Count 10)} Jan 11, 2020 at 6:23
  • Ricardo, that's not 'his' code; it was copied from an answer here. This is a disguised 'write me a script' request. Jan 11, 2020 at 10:21

2 Answers 2

2

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
1

I propose a simpler alternative

@echo off  
setlocal enabledelayedexpansion  
set "string=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"  
set prefix=0  
set lines=1000  
set chars=9  
set outfile=passwords.txt  
if exist "%outfile%" del "%outfile%"  
for /l %%A in (1,1,%lines%) do (  
    set "result="  
    for /L %%i in (1,1,%chars%) do call :addchar  
    echo %prefix%!result!  
    echo %prefix%!result! >> %outfile%  
    )  
goto end  

:addchar  
set /a x=%random% %% 52  
set result=%result%!string:~%x%,1!  
goto :eof  

:end  
echo Script finished  
pause  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.