1

after searching this site for "how to generate a password with CMD" I found some very useful examples. Then, I wanted to be able to print more then one password at a time (who would want to click all day on that) and I found some code that it can run the generator code for a defined number of time. What I want to do next, is to try and print all of the generated passwords in a one single .txt file. I know you have to use the "command > [drive:\path*.txt]" for that but, I don't know how :/. After I combined the generator and the loop code I got something like this, but it prints only one password in the file. When the other one is generated, the old one is replaced. Any help? :D

@echo off
set execute counter=0
:loop
(@Echo off  
rem 16 stings pwd

setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
set alfanum=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789

set pwd=
FOR /L %%b IN (0, 1, 16) DO (
SET /A rnd_num=!RANDOM! * 62 / 32768 + 1
for /F %%c in ('echo %%alfanum:~!rnd_num!^,1%%') do set pwd=!pwd!%%c
)

echo pwd=%pwd% > D:\password.txt
) :: This here between the () is the on line password generator
set /a executecounter=%executecounter%+1
if "%executecounter%"=="30" goto done
goto loop
:done
echo Complete!
pause

1 Answer 1

1

You are very close.

The > operator means: write to a textfile. If the textfile already exists, overwrite. If not, create a new file. This will undo your previous password.

The >> operator is what you need instead. It means: write to a textfile. If the textfile already exists, append to it. If not, create a new file.

So replace

echo pwd=%pwd% > D:\password.txt

with:

echo pwd=%pwd% >> D:\password.txt

If you want to create a blank line in that same file, add: echo. >> D:\password.txt

Your output will then be:

pwd=randompass

pwd=randompass

This is useful if you want to store more info besides the password, for example:

set foruser /p "for which user is this?"

echo -------------- >> d:\password.txt
echo Password generated at %date% %time%. >> d:\password.txt
echo Password was made for user: %foruser%
echo pwd=randompass >> d:\password.txt
echo -------------- >> d:\password.txt
echo. >> d:\password.txt
7
  • Not working. It just opened the prompt and it closed (running only once the generator), and if I open it again, it dosen't generate a new one. I need it to print 10, 40, 90 (what ever number I difine) passwords in the same file :(.
    – Unix
    Jul 21, 2015 at 19:20
  • Please check your code. I've coded a lot with .bat files and I know this by heart. it always worked for me, so if its not working, there must be something else going wrong.
    – LPChip
    Jul 21, 2015 at 19:22
  • I did just like you said... @echo off set execute counter=0 :loop (@Echo off rem 16 stings pwd setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION set alfanum=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 set pwd= FOR /L %%b IN (0, 1, 16) DO ( SET /A rnd_num=!RANDOM! * 62 / 32768 + 1 for /F %%c in ('echo %%alfanum:~!rnd_num!^,1%%') do set pwd=!pwd!%%c ) echo pwd=%pwd% >> D:\password.txt ) :: This here between the () is the on line password generator set /a executecounter=%executecounter%+1 if "%executecounter%"=="30" goto done goto loop :done echo Complete! pause
    – Unix
    Jul 21, 2015 at 19:45
  • Can you enter pause at the end of your batch file and run it, then check the output? any script error will show up at that point.
    – LPChip
    Jul 21, 2015 at 20:12
  • Also at the top, you write set execute counter=0. That space should be gone.
    – LPChip
    Jul 21, 2015 at 20:12

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.