1

I'm a beginner in batch programming. I want to create a batch script in order to create a random sequence of eight alphanumeric characters. This is my tentative:

@echo off
setlocal enabledelayedexpansion

::Initializing uppercase alphabet
set "upper=A B C D E F G H I J K L M N O P Q R S T U V W X Y Z"
set cntu=0 & for %%P in (!upper!) do (
    set /a cntu+=1
    set "upper[!cntu!]=%%P"
)

::Initializing lowercase alphabet
set "lower=a b c d e f g h i j k l m n o p q r s t u v w x y z"
set cntl=0 & for %%P in (!lower!) do (
    set /a cntl+=1
    set "lower[!cntl!]=%%P"
)

::Initializing numbers
set "numbers=0 1 2 3 4 5 6 7 8 9"
set cntn=0 & for %%P in (!numbers!) do (
    set /a cntn+=1
    set "numbers[!cntn!]=%%P"
)

::Initializing something...
for /L %%P in (0 1 8) do (
    set /a rndIntp=%random% %% 2
    if %rndIntp% == 0 (
        set /a rndIntu=%random% %% cntu +1
        set /a psw[!%%P!]=upper[%rndIntu%]
    )
    if %rndIntp% == 1 (
        set /a rndIntl=%random% %% cntl +1
        set /a psw[!%%P!]=upper[%rndIntl%]
    )
    if %rndIntp% == 2 (
        set /a rndIntn=%random% %% cntn +1
        set /a psw[!%%P!]=upper[%rndIntn%]
    )
)

pause

What's wrong? How can I outupt the created sequence? Thank you!

user3482381
  • 83
  • 1
  • 7
  • Maybe you should add the kind of shell to your tag so some one with experience with this kind of she'll script looks into this. I would they it's a windows batch script isn't it? – slowjack2k Oct 04 '17 at 20:33

1 Answers1

1

You can do this with a slight modification to Stephan's elegant solution:

@echo off
setlocal enabledelayedexpansion
set "string=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
set "result="
for /L %%i in (1,1,%1) do call :add
echo %result%
goto :eof

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

Save it, run it and pass the length of the random string you want:

RandomString.bat 10

Output:

yxfcK6rGWv

Edit: Updated solution based on refined requirements:

@echo off
setlocal enableextensions enabledelayedexpansion
set "string=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
set "upper=ABCDEFGHIJKLMNOPQRSTUVWXYZ"
set "numeric=0123456789"
set /a rand1=%random% %% 4
set /a rand2=rand1 + 4
set "result="
set /a index = 0
for /L %%i in (1,1,8) do (
    if !index!==%rand1% (
        call :addUpper
    ) else if !index!==%rand2% (
        call :addNumeric
    ) else (
        call :addAny
    )
    set /a index += 1
)
echo %result%
pause
goto :eof

:addUpper
set /a u=%random% %% 26
set result=%result%!upper:~%u%,1!
goto :eof

:addNumeric
set /a n=%random% %% 10
set result=%result%!numeric:~%n%,1!
goto :eof

:addAny
set /a s=%random% %% 62
set result=%result%!string:~%s%,1!
goto :eof
DSway
  • 752
  • 14
  • 33
  • Thank you. I saved it in a file with `.bat` extension, but if I try to run with double click on the icon, the Command Prompt window appears and disappears immediately... – user3482381 Oct 06 '17 at 17:17
  • 1
    That's how batch files work in general. You can add a pause command after the echo command to stop it. If you're going to run it that way, change the %1 to the length of string you want. – DSway Oct 07 '17 at 10:48
  • Thank you very much DSway. How could the script be modified in order to impose the condition that the elaborated string must contains at least a capital letter and at least a number? – user3482381 Oct 07 '17 at 12:18
  • 1
    Off the top of my head, set 2 new strings: 'upper' and 'numeric' then set random numbers like rand1 <= result length / 2 and rand2 = rand1 + result length / 2. Then you check the count in the loop and force the index at rand1 to be upper and the index at rand 2 to be a number. These posts show how to use count in a loop and generate random numbers: https://stackoverflow.com/questions/5777400/how-to-use-random-in-batch-script https://stackoverflow.com/questions/7522740/counting-in-a-for-loop-using-windows-batch-script – DSway Oct 07 '17 at 23:43
  • Sorry for disturbing and thank you again @DSway. I have implemented your previous indications and I read the posts you've reported. This is my attempt, in case I want to get a string of eight characters: – user3482381 Oct 08 '17 at 10:08
  • `@echo off setlocal enabledelayedexpansion set "string=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" set "upper=ABCDEFGHIJKLMNOPQRSTUVWXYZ" set "numeric=0123456789" set /a rand1=%random% %% 4 set /a rand2=rand1 + 4 set "result=" for /L %%i in (1,1,8) do call :add echo %result% pause goto :eof :add set /a x=%random% %% 62 set result[%rand1%]=upper[!%%i!] set result[%rand2%]=numeric[!%%i!] set result=%result%!string:~%x%,1! goto :eof` – user3482381 Oct 08 '17 at 10:08