Link to home
Start Free TrialLog in
Avatar of DevSupport
DevSupport

asked on

creating a random password in batch

I am trying to create a random password using batch.

I googled and found a method but it involves setlocal delayed exapansion and all which is affecting my existing code (with special characters and stuff)

This is the code I got:

@Echo Off
Setlocal EnableDelayedExpansion
Set _RNDLength=8
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 Random string is !_RndAlphaNum!


However I dont know how to store the result into a variable in say x so that I can get it as %x%

Like x=!_RndAlphaNum!

echo %x% can show me the random number generated.

unrelated question:

Is there a way to generate a random alpha-num string without using setlocal enabledelayedexpansion.
Avatar of Michael Pfister
Michael Pfister
Flag of Germany image

Add a SET command

SET X=!_RndAlphaNum!

Then use %X%, i.e.
ECHO %X%
SOLUTION
Avatar of Michael Pfister
Michael Pfister
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial