Why is the value of ErrorLevel not updating?

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

4 Answers

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

Your problem is that your second check is WITHIN the block of code that is used ONLY if your first check equals 0. You're like:

IF (we want to go out) THEN ( IF (it rains) THEN ( take umbrella )
)

See, the second check to see whether it rains is performed if, and ONLY if, we want to go out. If we don't want to go out, we don't bother checking for rain.

Solution: close the block after your first IF before you get to the second if, so you get:

IF %ERRORLEVEL% EQU 0 ( do stuff
)
IF %errorlevel% EQU 1 ( do other stuff
)

Or use an ELSE:

IF %ERRORLEVEL% EQU 0 ( do stuff
)
ELSE ( :: errorlevel was anything other than 0, i.e. NEQ 0 do other stuff
)
4

I don't have a windows machine easily available, but if it were me, I would verify that the ECHO statement does not change ERRORLEVEL.

Probably this is not related, but maybe you are using a command interpreter which treats uppercase and lowercase ERRORLEVEL differently? Even if that has no effect, it is peculiar to be inconsistent.

3

The following batch file will give you the status that you are looking for. You can easily adjust the gotos and labels to suit your needs.

Whether the file exists is checked, and ERRORLEVEL is updated appropriately.

@echo off
REM ---------------------------------------------------------------------------------
REM -- Check the status of Deep Freeze
set "DFC=C:\Program Files (x86)\Faronics\Deep Freeze Enterprise\DFC.exe"
if not exist "%DFC%" goto alwaysRun
"%DFC%" get /ISFROZEN >nul 2>&1
if [9009] == [%ERRORLEVEL%] goto alwaysRun
if ERRORLEVEL 1 goto isFrozen
if ERRORLEVEL 0 goto isThawed
REM ---------------------------------------------------------------------------------
REM ---------------------------------------------------------------------------------
REM -- Run all the commands when the workstation is frozen
REM -- (these commands are skipped when the workstation is thawed)
goto alwaysRun
:isFrozen
echo The workstation is frozen
REM ---------------------------------------------------------------------------------
REM ---------------------------------------------------------------------------------
REM -- Run all the commands when the workstation is thawed
REM -- (these commands are skipped when the workstation is frozen)
goto alwaysRun
:isThawed
echo The workstation is thawed
REM ---------------------------------------------------------------------------------
REM ---------------------------------------------------------------------------------
:alwaysRun
echo Always run this part
REM ---------------------------------------------------------------------------------
REM ---------------------------------------------------------------------------------
:end
pause

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like