Windows – is the value of ErrorLevel not updating

batch filedeep-freezewindows 7

I am trying to write a subroutine in batch to determine if a computer has Deep freeze installed or is safely unfrozen. (For those who don't know, Deep Freeze is a program that is commonly used to revert/prevent and disk changes in an Operating System.) dfc.exe is a file that deep freeze installs that can be used to determine. It returns 0 if thawed, or 1 if frozen

Basically, I was thinking that it's pointless to run scripts that install certain things if the computer is frozen. First I check for if dfc.exe is installed, then I attempt to check for thawed/frozen state but the problem IS that for some reason the return value of the dfc doesn't seem to update for where I check errorlevel the second time.

Anyone know why I can't see the return value of the second ErrorLevel check (on line 41) I've included the code below as well.

EDIT: Added Psuedocode for my thought process before the code.

::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) 

Code below here

@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

UPDATE: I gave up on the nested IFs and went back to GOTOs and Labels. It's not as pretty, but this code actually WORKS and in literally like ten minutes. I indented it to create the visual effect of the artificial "nesting"

@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)

Best Answer

ERRORLEVEL doesn't update inside control blocks like IF statements unless you use !ERRORLEVEL! instead of %ERRORLEVEL% and use this command at the start of your code: setlocal ENABLEDELAYEDEXPANSION

see http://batcheero.blogspot.ca/2007/06/how-to-enabledelayedexpansion.html