批次的ERRORLEVEL工作不正常和奇怪

时间:2017-12-22 09:53:10

标签: batch-file exit errorlevel

批处理时出现ERRORLEVEL问题。 我在这里粘贴两个代码片段来解决确切的问题,它可以重现。

我在这里没有粘贴的是我尝试使用延迟扩展的第二个代码片段,第一个使用IF ERRORLEVEL 0()而不是%ERRORLEVEL%== 0,但遗憾的是导致同样的问题。

首先尝试使用此代码:

@ECHO OFF

call :CheckFileExists .\Database.db3

echo CheckFileExists returned %ERRORLEVEL%
IF %ERRORLEVEL% == 0 (
    echo First check succeeded
    call :CheckFileExists .\dummy.txt

    echo CheckFileExists returned %ERRORLEVEL%
    IF %ERRORLEVEL% == 0 (
        echo Second check succeeded
    ) ELSE (
        echo Second check failed
    )
) ELSE (
    echo First check failed
)

GOTO :eof

:CheckFileExists
IF exist %1 (
    echo INFO: Necessary file exists: %1
) ELSE (
    echo ERROR: Necessary file missing: %1
    EXIT /B 1
)
EXIT /B

cmd中的结果:

INFO: Necessary file exists: .\Database.db3
CheckFileExists returned 0
First check succeeded
ERROR: Necessary file missing: .\dummy.txt
CheckFileExists returned 0
Second check succeeded

接下来,我为每个函数调用创建了两个变量来保存errorlevel,但是使用以下代码运行它失败(并使echo回显以找出它失败的地方):

@ECHO ON

call :CheckFileExists .\Database.db3

set error=%ERRORLEVEL%
echo CheckFileExists returned %error%
IF %error% == 0 (
    echo First check succeeded
    call :CheckFileExists .\dummy.txt
    set error2=%ERRORLEVEL%
    echo CheckFileExists returned %error2%
    IF %error2% == 0 (
        echo Second check succeeded
    ) ELSE (
        echo Second check failed
    )
) ELSE (
    echo First check failed
)

GOTO :eof

:CheckFileExists
IF exist %1 (
    echo INFO: Necessary file exists: %1
) ELSE (
    echo ERROR: Necessary file missing: %1
    EXIT /B 1
)
EXIT /B

为此,cmd中的结果:

call :CheckFileExists .\Database.db3

IF exist .\Database.db3 (echo INFO: Necessary file exists: .\Database.db3 )  ELSE (
echo ERROR: Necessary file missing: .\Database.db3
 EXIT /B 1
)
INFO: Necessary file exists: .\Database.db3

EXIT /B

set error=0

echo CheckFileExists returned 0
CheckFileExists returned 0
( was unexpected at this time.

        IF  == 0 (

我真的很喜欢这个,有人可以帮帮我吗?也许它可以是函数EXIT的东西?

提前致谢!

1 个答案:

答案 0 :(得分:2)

您需要delayed expansion

IF ERRORLEVEL

或使用@ECHO OFF call :CheckFileExists .\Database.db3 echo CheckFileExists returned %ERRORLEVEL% IF %ERRORLEVEL% == 0 ( echo First check succeeded call :CheckFileExists .\dummy.txt call echo CheckFileExists returned %%ERRORLEVEL%% IF ERRORLEVEL 0 IF NOT ERRORLEVEL 1 ( echo Second check succeeded ) ELSE ( echo Second check failed ) ) ELSE ( echo First check failed ) GOTO :eof :CheckFileExists IF exist %1 ( echo INFO: Necessary file exists: %1 ) ELSE ( echo ERROR: Necessary file missing: %1 EXIT /B 1 ) EXIT /B

<div style={{ display: "flex",  verticalAlign: "middle" }}>
    <Checkbox style={{ marginTop: 9, width: 40 }} checked={data.checked}
              onCheck={(e, value) => { this.updateTreeNodeCheckedById(data.id, value);}} />
    <Select value={data.value} simpleValue placeholder={data.label} multi
            style={{marginTop:"5px", width: "300px", maxWidth:"300px"}}       
            onChange={(value)=>{ this.updateTreeNodeValueById(data.id, value)}}
            options={[
                {value:0, label:"სატესტო1"},
                {value:1, label:"სატესტო2"},
                {value:2, label:"სატესტო3"},
                {value:3, label:"სატესტო4"}
            ]} /> 
    <IconButton onClick={() => { this.deleteTreeNode(data.id)}}>
        <ClearBtn />
    </IconButton>
</div>