creating a random password in batch

DevSupport
DevSupport used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Add a SET command

SET X=!_RndAlphaNum!

Then use %X%, i.e.
ECHO %X%
I'd say you have to use enabledelayedexpansion for the random password.
But if you don' t like it issue a

Setlocal DisableDelayedExpansion

after generating the password
And you can use the original password variable without assigning it to another variable

Setlocal DisableDelayedExpansion
ECHO %_RndAlphaNum%
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015
Commented:
The above recommendation does not persist outside of the batch file. Each setlocal starts a new set of (changed) variables, and changes will be reverted after endlocal or batch end.
One of the ways to allow for keeping results it by "exporting":
endlocal & set _RndAlphaNum=%_RndAlphaNum%

Open in new window

as last line should work - because the complete line is parsed as a whole, %var% replacement takes place (inserting the string value instead of the var), and then the line is executed.
Commented:
If you could use a few lines of VBScript embedded in your batch file try this.  Put any characters you want to use in the choice="" line and change for I=1 to 8 to a different number if you want longer.

Steve

@echo off
(echo choice="ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz"
echo password=""
echo Randomize
echo for i=1 to 8: password=password ^& mid^(choice,int^(rnd*len^(choice^)^)+1,1^) : next
echo wscript.echo password
) > "%temp%\make_password.vbs"

:start 
set password=
for /f %%a in ('cscript //nologo "%temp%\make_password.vbs"') do set password=%%a
echo Password this time is: %password%
pause
goto start

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial