为什么 ErrorLevel 的值没有更新?

Psy*_*ata 1 windows-7 deep-freeze batch-file

我正在尝试批量编写一个子例程,以确定计算机是否安装了 Deep freeze 或已安全解冻。(对于那些不知道的人来说,Deep Freeze是一个通常用于恢复/防止操作系统中的磁盘更改的程序。)dfc.exe是 Deep Freeze 安装的一个可用于确定的文件。如果解冻则返回 0,如果冻结则返回 1

基本上,我认为如果计算机被冻结,运行安装某些东西的脚本是毫无意义的。首先,我检查是否安装了 dfc.exe,然后尝试检查解冻/冻结状态,但问题是,由于某种原因,dfc 的返回值似乎没有更新我第二次检查错误级别的位置。

任何人都知道为什么我看不到第二个 ErrorLevel 检查的返回值(第 41 行),我也包含了下面的代码。

编辑:在代码之前添加了我的思考过程的伪代码。

::Point1
IF Dfc.exe is present then (
    ::Point2
    If "dfc get /ISFROZEN" returns FROZEN then (
        ::Point3
        Write out to file so we can know that something was skipped, 
        goto EOF to close out of script and avoid wasting cycles installing
    )
    ::Point4

::Deep freeze is installed, but thawed or it would have gotten caught in the "FROZEN" IF
::Fall through out of outer IF without running anything else
)
::Point5

GOTO (Return to where we left to check if we were wasting time with a label) 
Run Code Online (Sandbox Code Playgroud)

代码如下

@ECHO Off
::CheckForDF here
ECHO We will now test for DeepFreeze ether not installed or is thawed
Pause 
ECHO.

set flagfile=C:\testjava.txt
::Flagfile variable should already be defined before calling this function
Goto :CheckForDF
:DFNotFrozen
ECHO DeepFreeze wasn't installed or is currently thawed
ECHO **Continue with the rest of the script** 
ECHO.
PAUSE

GOTO:eof







::***************************
::********Subroutines********
::***************************


:CheckForDF
WHERE dfc >nul 2>&1


::ErrorLEvel 0 means file was found, which means DF is installed

IF %ERRORLEVEL% EQU 0 (
   dfc get /ISFROZEN
   ECHO %errorlevel%
   ::ErrorLevel 0 - THAWED and ready to install 
   ::ErrorLevel 1 - FROZEN and pointless to try

   IF %errorlevel% EQU 1 (

      ::Echo out what WOULD have been installed to a file so we could check that the
      ::   script still ran (and get an idea of how bad we need to unfreeze and log into each computer) 
      ECHO %flagfile% %date%%time% >> C:\BRCCIT\ScriptsSkippedByDeepFreeze.txt
      ECHO ****DeepFreeze is currently frozen****

      ECHO.
      PAUSE   
      ::Else - DeepFreeze is thawed. return to normal script
      goto :EOF

   )

   ::DeepFreeze is thawed, but is installed. We'll just fall through to the not installed result
)

::DeepFreeze Installation not found. Okay to return to normal script
ECHO DeepFreeze is not installed
goto :DFNotFrozen
Run Code Online (Sandbox Code Playgroud)

更新:我放弃了嵌套的 IF,回到了 GOTO 和标签。它不是那么漂亮,但这段代码实际上可以工作,而且实际上只需十分钟。我将其缩进以创造人工“嵌套”的视觉效果

@ECHO Off
::CheckForDF here
ECHO We will now test for DeepFreeze ether not installed or is thawed
Pause 
ECHO.

set flagfile=C:\testjava.txt
::Flagfile variable should already be defined before calling this function
Goto :CheckForDF
:DFNotFrozen
ECHO DeepFreeze wasn't installed or is currently thawed
ECHO **Continue with the rest of the script** 
ECHO.
PAUSE

GOTO:eof



::***************************
::********Subroutines********
::***************************



:CheckForDF
WHERE dfc >nul 2>&1

IF %ErrorLevel% EQU 0 Goto :DFInstalled
::ELSE - DF Not  found 
GOTO :ReturnToScript


    :DFInstalled
    ::DFC.exe get /ISFROZEN
    Call ExitWithSpecifiedCode.bat 1
    ::If Frozen
    IF %ErrorLevel% EQU 1 GOTO DFFrozen
    ::If Thawed
    IF %ErrorLevel% EQU 0 GOTO ReturnToScript


        :DFFrozen
        ECHO %flagfile% %date%%time% >> C:\BRCCIT\ScriptsSkippedByDeepFreeze.txt
        ECHO ****DeepFreeze is currently frozen****

        ECHO.
        PAUSE
        ::Exit Script Execution
        goto :EOF

:ReturnToScript
ECHO ReturnToScript
PAUSE
GOTO (Return to script execution)
Run Code Online (Sandbox Code Playgroud)

小智 6

ERRORLEVEL 不会像 IF 语句那样更新内部控制块,除非您使用 !ERRORLEVEL! 而不是 %ERRORLEVEL% 并在代码开头使用此命令:setlocal ENABLEDELAYEDEXPANSION

请参阅http://batcheero.blogspot.ca/2007/06/how-to-enabledelayedexpansion.html