Stuck (Incorrect) ERRORLEVEL

K

kick

I'm putting together a fairly simply batch file (to be run under XP)
that

-checks for a registry value (REG QUERY)
-if the value doesn't exist, add it (REGEDIT /S or REG ADD /F... tried
both)
-checks for the registry value again (REG QUERY)
-if the value exists now, write entry to success log otherwise, write
entry to failure log

After the calls to REG QUERY i'm just checking %errorlevel% - 0 = the
value exists and 1 (or not 0) = the value doesn't exist.

The first call to REG QUERY returns the correct %errorlevel% -
confirmed by checking the registry manually prior to running the
script. The subsequent call to REG QUERY returns the wrong value for
%errorlevel%. In fact, it returns the same value as what the first
call to REG QUERY returned. Thoughts? Nowhere in my script and I
setting the value of ERRORLEVEL... it's as if the ERRORLEVEL value is
being retained or is "stuck".

My scripts is:

@ECHO OFF
CLS

REG QUERY "HKLM\SOFTWARE\MyKey" /V "MyValue"

IF %ERRORLEVEL% NEQ 0 (

REGEDIT /S My.reg

REG QUERY "HKLM\SOFTWARE\MyKey" /V "MyValue"

IF %ERRORLEVEL% EQU 0 (
ECHO %date%,%time%,%computername% >> success.log
) ELSE (
ECHO %date%,%time%,%computername% >> failure.log
)
)

EXIT
 
P

Pegasus \(MVP\)

kick said:
I'm putting together a fairly simply batch file (to be run under XP)
that

-checks for a registry value (REG QUERY)
-if the value doesn't exist, add it (REGEDIT /S or REG ADD /F... tried
both)
-checks for the registry value again (REG QUERY)
-if the value exists now, write entry to success log otherwise, write
entry to failure log

After the calls to REG QUERY i'm just checking %errorlevel% - 0 = the
value exists and 1 (or not 0) = the value doesn't exist.

The first call to REG QUERY returns the correct %errorlevel% -
confirmed by checking the registry manually prior to running the
script. The subsequent call to REG QUERY returns the wrong value for
%errorlevel%. In fact, it returns the same value as what the first
call to REG QUERY returned. Thoughts? Nowhere in my script and I
setting the value of ERRORLEVEL... it's as if the ERRORLEVEL value is
being retained or is "stuck".

My scripts is:

@ECHO OFF
CLS

REG QUERY "HKLM\SOFTWARE\MyKey" /V "MyValue"

IF %ERRORLEVEL% NEQ 0 (

REGEDIT /S My.reg

REG QUERY "HKLM\SOFTWARE\MyKey" /V "MyValue"

IF %ERRORLEVEL% EQU 0 (
ECHO %date%,%time%,%computername% >> success.log
) ELSE (
ECHO %date%,%time%,%computername% >> failure.log
)
)

EXIT

Windows treats the code starting with the first "If %ErrorLevel%"
statement and ending with the last parenthesis as one single line.
Single lines are scanned ***once*** to resolve variables, then
executed. This means that the variable %ErrorLevel% will ***not***
change inside this long line.

You have three options to resolve the problem:
- Instead of using %ErrorLevel%, use the "if Errorlevel" syntax.
- Instead of using multi-line code, use subroutines.
- Enable delayed variable expansion like so:

@ECHO OFF
setlocal enabledelayedexpansion
REG QUERY "HKLM\SOFTWARE\MyKey" /V "MyValue"
IF %ERRORLEVEL% NEQ 0 (
REGEDIT /S My.reg
REG QUERY "HKLM\SOFTWARE\MyKey" /V "MyValue"
IF !ERRORLEVEL! EQU 0 (
ECHO %date%,%time%,%computername% >> success.log
) ELSE (
ECHO %date%,%time%,%computername% >> failure.log
)
)
endlocal
 
K

kick

I was even aware of env variable localization... time to do a bit of
reading. I hit some of Microsoft's but it's pretty minimal. Doesn't
even discuss the ! notation you've used around the second evaluation
of ERRORLEVEL.

Thanks for the tip.
 
P

Pegasus \(MVP\)

kick said:
I was even aware of env variable localization... time to do a bit of
reading. I hit some of Microsoft's but it's pretty minimal. Doesn't
even discuss the ! notation you've used around the second evaluation
of ERRORLEVEL.

Thanks for the tip.

All command line commands are discussed in detail at
the Command Prompt. In your case it's set /?.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top