Batch 的 ERRORLEVEL 工作不正常且很奇怪
Batch's ERRORLEVEL works improperly and weird
我在批量处理 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
接下来,我为每个函数调用创建了两个变量来保存错误级别,但是 运行 它因以下代码而失败(并进行回显以找出它失败的地方):
@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 (
我真的被这个问题困住了,有人能帮我吗?可能是函数的退出?
提前致谢!
你需要 delayed expansion:
@ECHO OFF
setlocal enableDelayedExpansion
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
或使用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
我在批量处理 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
接下来,我为每个函数调用创建了两个变量来保存错误级别,但是 运行 它因以下代码而失败(并进行回显以找出它失败的地方):
@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 (
我真的被这个问题困住了,有人能帮我吗?可能是函数的退出?
提前致谢!
你需要 delayed expansion:
@ECHO OFF
setlocal enableDelayedExpansion
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
或使用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