-1

I am trying to make a program that makes a series of random letters and numbers, upper-case and lower case. Is there a way that I can make this possible for my code?

@echo off
title random password gen
:Start
cls && set choice=0
echo (type 0 to exit)
echo.

echo 1 Random Password
echo 5 Random Passwords
echo 10 Random Passwords

set /p input= quantity:
if %input%==0 exit
if %input%==1 set /a choice=%choice%+1 && goto a
if %input%==2 goto set /a choice=%choice%+5 && goto a
if %input%==3 goto set /a choice=%choice%+10 && goto a
goto start
:a

cls

if %choice%==1 echo your password is %random%
if %choice%==5 echo your 5 passwords are %random%, %random%, %random%, %random%, %random%.
if %choice%==10 echo your 10 passwords are %random%, %random%, %random%, %random%, %random%,%random%, %random%, %random%, %random%, %random%.
pause
echo press any key to continue to menu... && pause nul> && goto menu
dirtydanee
  • 6,081
  • 2
  • 27
  • 43
Topik
  • 11
  • 3

1 Answers1

0
  • You are using the conditional execution on success && wrongly. To have two commands on one line use only one &
  • %Random% returns a number between 0 and 32768. See ss64.com/nt/syntax-random.html
  • The var choice is a bit cumbersome as there is an external program choice.exe
  • If you don't store %random% to a var it is lost, next time %random% gets a different value.

The following batch will generate a random Name/Passwort

@Echo off&SetLocal EnableExtensions EnableDelayedExpansion
Set "Chars=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"

:: get one char from the first 26(Uppercase) and append to var Name
Call :RandChar 26 Name

:: get 14 chars from all Chars and append to var Name
For /L %%A in (1,1,14) Do Call :RandChar 62 Name

Echo %Name%
Goto :Eof

:RandChar Range Var
Set /A Pnt=%Random% %% %1
Set %2=!%2!!Chars:~%Pnt%,1!
Goto :Eof

The same in powerShell is a bit simpler:

$chars = [char[]]"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
$Password = [string](($chars[0..25]|Get-Random)+(($chars|Get-Random -Count 14) -join ""))