My batch programm crashed/closes when it checks the error level

52 views Asked by At

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.

2

There are 2 answers

0
Magoo On

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 syntax set 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 pause after 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 choice which is designed for the option-choice task. Use the search facility for [batch-file] choice eg. Gerhard's example or see the documentation - choice /? from the prompt.

Your problem is that repeat is set to nothing if there is no error, so the command if %repeat%==true cls is resolved to if ==true cls which 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 by for %%j in (!index!)

0
jeb On

If you enable delayed expansion you should use it.
Your problem of the password treatment as a command is a problem of the percent expansion in echo Generiertes Passwort: %password%, use echo Generiertes Passwort: !password! instead.

If you check your code with set characters you will see that some expected characters are not in your string. You have to define it better with:

set "characters=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789^!@#$%%^&*()_-+=<>?"
REM Check the characters
set characters

With this you don't need to check the errorlevel.
But you should fix all of your IF %var%==value lines to IF "%var%"=="value" or even better IF "!var!"=="value"