-2

I'm programming and I'm having an issue.

if exist savefile.climax (
echo It appears you have one...
echo Checking your data...
(
set /p name=
set /p level=
)<savefile.climax
) else (
echo Oh man, you don't have one.
echo Would you like to create it?
choice /c yn /n /m "[Y]es or [N]o?"
if %errorlevel% == 1 goto creation
if %errorlevel% == 2 exit
)

And as you can see, if the errorlevel is 2 the program should exit, but it doesn't. It keeps going on to the creation code. How could I fix this issue?

Richhh_y
  • 79
  • 7
  • Nope, could not find the answer there. Too complicated for me. :/ – Richhh_y Mar 22 '19 at 17:50
  • Well I will tell you right now if you don't understand how variables work inside a parentheses code block, you are going to have a very difficult time programming batch files. That is the question that most of us link to when we see people who do not understand that concept. All you would need to do is enable delayed expansion with this command: `setlocal enabledelayedexpansion` and then change `%errorlevel%` to `!errorlevel!`. – Squashman Mar 22 '19 at 18:31

2 Answers2

0

The reason why you never reached the exit is because your %errorlevel% gets set and used inside a code block, you therefore needed to enabledelayedexpansion. That said, you could get away without it:

@echo off
if exist savefile.climax (
    echo It appears you have one...
    echo Checking your data...
    (
    set /p name=
    set /p level=
    )<savefile.climax
) else (
    echo Oh man, you don't have one.
    echo Would you like to create it?
    choice /c yn /n /m "[Y]es or [N]o?"
    if errorlevel 2 exit
    if errorlevel 1 goto creation
)
Gerhard
  • 22,678
  • 7
  • 27
  • 43
  • And here is the [duplicate answer](https://stackoverflow.com/a/8616822/1417694) that addresses the code change you also provided. – Squashman Mar 22 '19 at 18:36
  • @Richhh_y, as an additional note, `YN` is the default for the `Choice` command, as such you do not really need to use its `/C`, `/N` or `/M` options, `Choice` alone should be sufficient. – Compo Mar 22 '19 at 18:49
  • @Compo yes, I simply did not change that to keep OP's default code, but worth mentioning. – Gerhard Mar 22 '19 at 18:50
-1

I found a solution by myself. I put the

if %errorlevel% == 1 goto creation
if %errorlevel% == 2 exit

out of the code block, like this.

if exist savefile.climax (
echo It appears you have one...
echo Checking your data...
(
set /p name=
set /p level=
)<savefile.climax
) else (
echo Oh man, you don't have one.
echo Would you like to create it?
choice /c yn /n /m "[Y]es or [N]o?"
)

if %errorlevel% == 1 goto creation
if %errorlevel% == 2 exit
Gerhard
  • 22,678
  • 7
  • 27
  • 43
Richhh_y
  • 79
  • 7
  • What happens with an `ErrorLevel` of `0`? – Compo Mar 22 '19 at 20:49
  • You really want that code executing when the `IF` command is true? – Squashman Mar 23 '19 at 00:24
  • This works for me perfectly. @Compo Uhmm.. Choice stores it's data on %errorlevel% and it appears that it is empty. – Richhh_y Mar 23 '19 at 06:58
  • @Squashman Yes, I want to. – Richhh_y Mar 23 '19 at 06:59
  • Let me put it another way,; with respect to the `%ErrorLevel%` lines and the line which follows it, what happens when `savefile.climax` exists? – Compo Mar 23 '19 at 10:27
  • Your code will still execute the IF commands that check the error level when the previous IF command to check for file existence is true. Pretty sure you don't want that to happen. – Squashman Mar 23 '19 at 14:09
  • 1
    I have downvoted your answer, because of the issue commented by above by both myself and @Squashman. The other current answer, should work without those issues, and it would be wrong to allow an incorrect answer to be marked as accepted without highlighting that fact to future readers. – Compo Mar 25 '19 at 09:02