开发者

Trying to understand the logic and "crutches" of errorlevel

I have it here .bat file and I'm trying to figure something out. My brain is just boiling from trying to realize what is wrong here!

Here is my code:

echo off
setlocal
cls

:perm_ask
echo Make a permanent format? 
CHOICE /C YN /N /T开发者_如何转开发 15 /D N /M "Press Y for Yes and N for No: "
IF ERRORLEVEL 1 set perm=TRUE
IF ERRORLEVEL 2 set perm=FALSE

goto select_format

:select_format
cls
if "%perm%"=="TRUE" (echo You have selected the permanent save mode. Be CAREFUL! & echo.):: checking for truth

echo Select the file format:
echo =============
echo 1) .fb2 format
echo 2) .epub format
echo 3) .fb2 + .epub formats

CHOICE /C 123 /N /M "Format: "

IF ERRORLEVEL 1 set form=fb2
IF ERRORLEVEL 2 set form=epub
IF ERRORLEVEL 3 set form=fb2,epub

goto url_insert

:url_insert
cls
if "%perm%"=="TRUE" (echo TRUEEEE) else (echo FALSEEE):: checking for truth

if "%form%"=="fb2,epub" (echo You have chosen the .epub and .fb2 format
    ) else (echo You have chosen the .%form% format)
echo. 
set /p url=Insert the URL: 

Elib2Ebook.exe -u %url% -f %form%

if "%perm%"=="TRUE" (goto url_insert) else (goto ask_cont)

:ask_cont
echo. 
set ERRORLEVEL=0:: trying to reset a huge negative value
CHOICE /C YN /M "Continue? "
IF ERRORLEVEL 1 goto select_format
IF ERRORLEVEL 2 goto exit

:exit
@exit

In perm_ask I ask whether to set a permanent file format.

In select_form I assign values to form

In url_insert I insert the link and the execution of the program is done

In ask_count, if I answered no in perm_ask, it is asked to continue or exit...

After all, if I don't change the order, even if I'm in Continue? the answer is No, it still goes to select_form

And so the question is. If I had everything in order with ERRORLEVEL before, I didn't even need to arrange it in reverse order (BECAUSE IT DOESN'T WORK FOR SOME REASON!). Then in url_insert I have a problem with Continue? if not to change the order or not to do so:

IF %ERRORLEVEL% == "1" goto select_format
IF %ERRORLEVEL% == "2" goto exit

So here's the question... why? Why is everything fine before that, but then you either need to change the order, or assign ERRORLEVEL to a variable? I tried changing the order for ERRORLEVEL in other places (perm_ask and select_form), but it only broke everything!


Originally, there was one syntax for if errorlevel which was

if errorlevel n dosomething

which was, and is interpreted as if errorlevel is n OR GREATER THAN n.

Consequently, testing needed to be done in reverse order of errorlevel and spaghetti-code was generated to have the various expected errorlevels do multiple things.

Later, errorlevel was introduced as a magic variable, set by the system, and else was made available to complement if.

Backward compatility dictates that the old functionality needs to be maintained to avoid having to re-write existing programs.

So - you choose to use %errorlevel% or the original syntax as suits your situation.


You've got multiple things going on here.

The syntax if errorlevel 1 means "if %errorlevel% is 1 or higher" so if errorlevel 1 will always be triggered by choice if it is the first thing. This is the reason that they should be arranged in descending order.

Additionally, if statements include quotes in the comparison, so both IF %ERRORLEVEL% == "1" and IF %ERRORLEVEL% == "2" return false, since 1 is not equal to "1". If you want to use an if statement, put quotes around both things:

IF "%ERRORLEVEL%" == "1" goto select_format
IF "%ERRORLEVEL%" == "2" goto exit
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜