-1

I've written a batchfile and for hours I'm trying to fix a problem where a choice-command would cause the program to crash. The crash seems to occur during the first "if ERRORLEVEL"-command.

    choice /c EDCBA /n
if "%ERRORLEVEL%"=="5" (
    cls
    echo From which file should the first line be extracted?
    set /p ffe="> "
    echo set /p %gclvar%="<%ffe%">>codes/%current%.bati
    goto createblock
)
if "%ERRORLEVEL%"=="4" (
    echo set /p %gclvar%="> ">>codes/%current%.bati
    goto createblock
)
if "%ERRORLEVEL%"=="3" (
    cls
    echo Type in the operation.
    set /p op="> "
    echo set /a %gclvar%=%op%>>codes/%current%.bati
    goto createblock
)
if "%ERRORLEVEL%"=="2" (
    cls
    echo Type in the variable. (without "%")
    set /p var="> "
    echo set %gclvar%=%%var%%>>codes/%current%.bati
    goto createblock
)
if "%ERRORLEVEL%"=="1" goto createblock

The variable %gclarvar% is set by the user (no spaces used). The variable %current% is created with "%random%(block)". (:createblock exists)

  • `%ffe%` has not the value entered by the user during the codeblock. what string is `%gclvar%` or `%current%` being defined with? what error output do you get when running the batch file with `@echo on`? Note: the line ` echo Type in the variable. (without "%")` has an unescaped closing parentheses which will close to codeblock and leave and unbalanced parantheses, however in this instance is not the cause of the script failure - tbc... – T3RR0R Oct 27 '21 at 18:24
  • In fact, as it's currently posted it doen't crash, however there are issues with variable expansion to during codeblocks to be resolved using delayed expansion and escaping of characters such as `%` using `%%` and `)` using `^)` required to achieve your expected output. additionally, best practice with redirection is to place the redirection ahead of the output string and specidiy the stream to be redirected. `1>>"filename.ext" Echo(String to output` - escpaping poison characters such as `>` `<` `&` `|` and `)` in the Echo command. – T3RR0R Oct 27 '21 at 18:27

1 Answers1

-5

You’re treating ERRORLEVEL as a string; it’s a numeric.

Instead of IF "%ERRORLEVEL%" == "5" ... etc., which does a string comparison, use either

IF ERRORLEVEL 5 ... which tests ERRORLEVEL using a mechanism that has existed since more years ago than I care to admit to knowing about,

or

IF %ERRORLEVEL% EQU 5 ... which tests

See SS64 on CHOICE and SS64 on IF

Jeff Zeitlin
  • 9,773
  • 2
  • 21
  • 33
  • 1
    There is no difference between the integer `5` and the string `5` in batch. – SomethingDark Oct 27 '21 at 22:12
  • 2
    @SomethingDark: Strictly speaking, there is **not** _integer_ data in Batch files. All variables are strings... – Aacini Oct 27 '21 at 23:42
  • @Aacini - I considered saying that, but `set /a` actually does temporarily convert the value to a signed 32-bit integer before it does math and then just converts it back to a string. I feel like I also saw a thread on DosTips somewhere about two or three other places that it's briefly an integer. – SomethingDark Oct 28 '21 at 18:00
  • 1
    @SomethingDark: Yes. The key word here is _temporarily_. The integer just exists while the SET /A command (and others) is executed, but all variables just contain strings. I even use this phrase as a counter-argument for people that criticize me when I call "array" these variables: `vec[%i%]`. They say that, _strictly speaking_, Batch-files did _not_ manage arrays and such a notation is just a _simulation_. Well, I can assure that in Batch-files there are **not** numeric variables, just strings, and that SET /A command is "a numeric simulation". **`¯\_(ツ)_/¯`** – Aacini Oct 30 '21 at 00:26
  • For what it's worth, I completely agree that `var[%element%]` is an array :) – SomethingDark Oct 30 '21 at 01:51
  • @SomethingDark: **`:)`** – Aacini Nov 09 '21 at 19:40