Ive programmed a little Password Generator, so that I can simply run the programm to get a password in any size with just the characters I want, but when I run it, sometimes it thinks that some of the randomized characters is a command, which it cant find. Since this ruins the password output and I couldnt figure the problem out, I just made a line, which checks the error Level and then restarts the Program, but now when it checks the Error Code, it just crashes/closes. I cant figure that problem out, so if someone could please help me?
@echo off
color a
setlocal enabledelayedexpansion
REM Benutzer gibt die Passwortlaenge ein
set /p length=Geben Sie die Passwortlaenge ein:
REM Hier wieder einsteigen (Zeile 8)
:repeat
set password=
set timeout=
set repeat=
REM Zeichen für das Passwort
set "characters=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_-+=<>?"
REM Initialisiere das Passwort
set "password="
REM Generiere das Passwort
for /L %%i in (1,1,%length%) do (
set /a index=!random! %% 67
for /l %%j in (!index!,1,!index!) do set "char=!characters:~%%j,1!"
set "password=!password!!char!"
)
::if errorlevel 1 set repeat=true
REM Error wiederholung
if %repeat%==true cls
if %repeat%==true goto repeat
REM Zeige das generierte Passwort an
echo Generiertes Passwort: %password%
REM Wie lange soll angezeigt werden?
set /p timeout=Wie lange soll das Passwort angezeigt werden (in Sekunden)?
timeout %timeout% >NUL
REM Soll noch ein Passwort generiert werden?
set /p repeat=Noch eins? j/n
if %repeat%==j goto repeat
echo Schliesse das Programm in 10 Sekunden...
timeout 10
endlocal
Just want it to restart the password generation properly, when the ERRORLEVEL is grater than 0.
Tip: Use
set "var=value"for setting string values - this avoids problems caused by trailing spaces. Don't assign"or a terminal backslash or Space. Build pathnames from the elements - counterintuitively, it is likely to make the process easier. If the syntaxset var="value"is used, then the quotes become part of the value assigned.When you use the point-click-and-giggle method of executing a batch, the batch window will close if a syntax-error is found or the script runs to completion. You can put a
pauseafter statements & home in on the error, but better to open a 'command prompt' & run your batch from there so that the window remains open & any (error) messages will be displayed.please consider
choicewhich is designed for the option-choice task. Use the search facility for[batch-file] choiceeg. Gerhard's example or see the documentation -choice /?from the prompt.Your problem is that
repeatis set to nothing if there is no error, so the commandif %repeat%==true clsis resolved toif ==true clswhich is a syntax error.Using Boolean
Be careful of the use of
%and!(especially) as they have special meanings in batch. There is also special meaning assigned to^&*()=<>?Your string is 79 characters long, not 67.
for /l %%j in (!index!,1,!index!)could be replaced byfor %%j in (!index!)