0

Is it possible to write multiple Variables in one .txt document in batch? I would like to make a random password generator where you first have to say how many characters the password must be long and then the password gets generated and put in a .txt file

My idea was that first (after you said how long the password should be) a random number is generated (for the start 1, 2 or 3 (1 = a, 2 = b, 3 = c)). Then it looks which number was chosen and then the corresponding letter is searched and written into the txt document until it has as many characters as you said at the beginning.

That would look like that:

@echo off

:main
cls
set /p anz=How many characters?: 
goto rand
:rand
set /a letter=%random% %%3
goto test

:test
if %letter%==1 goto 1
if %letter%==2 goto 2
if %letter%==3 goto 3

:1
if %anz%==0 goto finish
set /p print=a
set /a anz-=1
goto printin

:2
if %anz%==0 goto finish
set /p print=b
set /a anz-=1
goto printin

:3
if %anz%==0 goto finish
set /p print=c
set /a anz-=1
goto printin

:printin
echo %print% > Your_Password.txt                   <--- Here does the letter get written in the .txt file
goto rand

:finish
echo finish
goto main

But it only writes the last letter in the .txt file

For the beginning i have only made it with a, b, c in future i want to add the entire Alphabet

I am quite new in batch and collecting first my first experiences

  • `>` overwrites the contents of the text file, `>>` appends to the end of the text file. Also, `echo` adds a newline so each letter will be on its own separate line unless you do `Your_password.txt`. Also also, `%random% %% 3` will return a number between 0 and 2 so you need to either adjust your numbers accordingly or stick a `+1` to the end of that equation. – SomethingDark Dec 07 '22 at 15:17
  • What do you mean with Your_password.txt do i need to set that behind the line or replace that with the line? – Redstone Guy Dec 07 '22 at 15:27
  • Do `>Your_password.txt` instead of `echo %print% > Your_Password.txt` – SomethingDark Dec 07 '22 at 15:56
  • Now in the Your_Password.txt is written: ECHO ist ausgeschaltet (OFF). Do you know who to fix that? – Redstone Guy Dec 07 '22 at 16:03

1 Answers1

0

It's better to first collect all needed characters in a variable and write to the file just once:

@echo off
setlocal enableextensions enabledelayedexpansion
REM valid chars for password:
set "chars=abcdefghijklmnopqrstuvwxyz"
REM number of chars:
set "CharCount=26"
REM clear pwd: 
set "pwd="
set /p "anz=How many characters?: "
REM %anz% times, add a random char:
for /l %%i in (1,1,%anz%) do call :addChar
(echo %pwd%)>"YourPassword.txt"
goto :eof

:addChar
rem get a random number [0..anz-1]
set /a rnd=%random% %% %CharCount%
REM add the rnd-th character from validChars to the pwd (zerobased count):
set "pwd=%pwd%!chars:~%rnd%,1!"
goto :eof

See set /? for how the random character is selected and for /? to learn how it does it anz times.
Also read about delayed expansion for why ! is used.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Stephan
  • 53,940
  • 10
  • 58
  • 91