choice and errorlevel

January 24, 2010 at 15:12:13
Specs: Windows 7 Ultimate x64, 4 GB
This may seem like a long code, but I'm only having problems with one line.

I have two batch scripts.

First one that works as it should:

About that cracking part, I own all these games,
and I'm just making a DVD backup of these 3 CD games.
Don't worry, nothing illegal here. :P

Second one behaves somehow strange:

Everything except one single line in each script isn't necessarily, but I posted it just in case something has an effect on this error.

Now to explain what's wrong.
Line 124 in first script is the same as line 19 in the second one.

But they return different errorlevel.

Line 124 in first script return 1 for Y, and 2 for N, just like it should.

And line 19 in second script returns 0 for both Y and N.

Command are the same, and yet they return different errorlevel.

Why is that so, and how to fix that second script.

See More: choice and errorlevel

Report •

January 24, 2010 at 17:39:11
i've never run across those options of choice, so they must be new for windows 7. make sure both scripts are running the same version of "choice", and if so, consider reverting to an older "model" of choice and see if that works since all it's doing is asking "y/n" anyhow (no need for anything fancy like whatever the ˔ does). I would get ahold of
i doubt if it's anything in the scripts themselves.
as last resort, contact the scripts author(s) for help resolving.

Report •

January 24, 2010 at 18:14:47
I don't know what options of choice you encountered, but this is the most common way of using it, i.e. checking errorlevel based on user input. This is not new for Windows 7.

I also use delay option of choice to make delays in script, but that doesn't matter.

I just can't figure out why the second one doesn't work as it should, why does it always returns 0, despite the user input.

By the way, I'm the author of these scripts.
As I sad, I'm creating custom installer that will allow me to install all three games at once.

And yes, that little dot char (˙), if you wonder what it does. Without it, all the leading space characters would be ignored. I used this one because it's small, but it can be replaced with any other character.

Report •

January 24, 2010 at 19:44:41
well, specifically, i've never seen optionswitch /m, but like i say, who knows as i'm way behind on everything.
as far as i could tell, the scripts are identical at the point of "choice..." (I had to enlarge the text in notepad, my eyesight sucks these days). I am curious as to how they could behave inconsistantly, esp if A) you're running them in identical setup: same computer, same opsys, same directory and B) the environment is identical for both (f/e: things like enableextensions etc., which i highly doubt are affecting this case.).
keep posted, i'm curious... mebbe somn es cn hep here!

Report •

Related Solutions

January 25, 2010 at 00:26:31
Both A and B are as you said. Same PC, same folder.
And yet second one always returns errorlevel equal to 0.

I hope someone will be able to help.

Report •

January 25, 2010 at 17:29:29
I discovered that if I delete line 16 (second script) choice command will return 1 and 2 just like it should.

And also, in first script, choice isn't enclosed in brackets of if command.

So, I came to conclusion that choice command doesn't work inside if command.

I made a little script just to verify that:

if 1==1 (
echo %errorlevel%

And choice command again returned errorlevel of 0 for any input.

...seems I'll have to use other commands for my script to work.
Using set command and checking for value of variable will work just fine.

Thanks for help nbrane. :)

Report •

January 25, 2010 at 17:40:14
So, I came to conclusion that choice command doesn't work inside if command.
This conclusion is incorrect.
C:\>set /?
 . . . . . . . . . 
Delayed environment variable expansion is useful for getting around
the limitations of the current expansion which happens when a line
of text is read, not when it is executed.  The following example
demonstrates the problem with immediate variable expansion:

    set VAR=before
    if "%VAR%" == "before" (
        set VAR=after
        if "%VAR%" == "after" @echo If you see this, it worked

would never display the message, since the %VAR% in BOTH IF statements
is substituted when the first IF statement is read, since it logically
includes the body of the IF, which is a compound statement.  So the
IF inside the compound statement is really comparing "before" with
"after" which will never be equal.  Similarly, the following example
will not work as expected:

    set LIST=
    for %i in (*) do set LIST=%LIST% %i
    echo %LIST%

in that it will NOT build up a list of files in the current directory,
but instead will just set the LIST variable to the last file found.
Again, this is because the %LIST% is expanded just once when the
FOR statement is read, and at that time the LIST variable is empty.
So the actual FOR loop we are executing is:

    for %i in (*) do set LIST= %i

which just keeps setting LIST to the last file found.

Delayed environment variable expansion allows you to use a different
character (the exclamation mark) to expand environment variables at
execution time.  If delayed variable expansion is enabled, the above
examples could be written as follows to work as intended:
 . . . . . . . . . . . .

Report •

January 25, 2010 at 18:40:35
I see now. Choice inside if would work if I enable delayed environment variable expansion and use '!' instead of '%'.

Without delayed expansion enabled, errorlevel had the value of 0 because it wasn't errorlevel of choice command, but the if command.

Thanks for clearing that up. :)

Report •

Ask Question