【问题标题】:Trying to understand the logic and "crutches" of errorlevel试图理解errorlevel的逻辑和“拐杖”
【发布时间】:2022-12-07 09:26:02
【问题描述】:

我这里有 .bat 文件,我想弄清楚。我的大脑因为试图意识到这里出了什么问题而沸腾了!

这是我的代码:

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

perm_ask问是否设置永久文件格式。

select_form中我给form赋值

url_insert中插入链接,程序执行完毕

ask_count中,如果我在perm_ask中回答否,则要求继续或退出...

毕竟,如果我不更改顺序,即使我在Continue? 答案是否定的,它仍然会转到select_form

所以问题是。如果我之前用 ERRORLEVEL 把所有东西都按顺序排列,我什至不需要以相反的顺序排列它(因为它出于某种原因不起作用!)。然后在 url_insert 我对 Continue? 有疑问,如果不更改顺序或不更改顺序:

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

那么问题来了……为什么?为什么在那之前一切都很好,但之后您要么需要更改顺序,要么将 ERRORLEVEL 分配给变量?我尝试在其他地方(perm_askselect_form)更改 ERRORLEVEL 的顺序,但它只是破坏了一切!

【问题讨论】:

    标签: windows batch-file cmd command-prompt logical-operators


    【解决方案1】:

    你在这里发生了很多事情。

    语法 if errorlevel 1 表示“如果 %errorlevel% 为 1或更高" 所以if errorlevel 1如果是第一件事,总是会被选择触发。这就是它们应该按降序排列的原因。

    此外,if 语句在比较中包含引号,因此两个都IF %ERRORLEVEL% == "1"IF %ERRORLEVEL% == "2" 返回 false,因为 1 不等于 "1"。如果你想使用 if 语句,请在两边加上引号:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-09
      • 1970-01-01
      • 2021-07-09
      • 1970-01-01
      • 1970-01-01
      • 2022-07-06
      • 2017-12-13
      • 1970-01-01
      相关资源
      最近更新 更多