Why does %errorlevel% not work with an "if" statement?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Why does %errorlevel% not work with an "if" statement?

#1 Post by MKANET » 14 Apr 2012 13:43

Okay, this is really frustrating... :cry:

If I use the following three lines by themselves, it works as expected:

Code: Select all

find /c "<errors>0</errors>" %recentBK%
if %errorlevel% equ 1 set today=failed
echo %today%


As soon as I put this within a condition, the %errorlevel% command no longer works (see below). I am certain the condition is met before expecting the stuff within the parenthesis to work. How can I get %errorlevel% to behave correctly within a condition statement? Or, at least find the correct way to get the below logic to work:

Code: Select all

if %BKfiledate%==%date% (
    find /c "<errors>0</errors>" %recentBK%
    if %errorlevel% equ 1 set today=failed
    echo %today%
)

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Why does %errorlevel% not work with an "if" statement?

#2 Post by Squashman » 14 Apr 2012 14:05

Once you have it inside parenthesis to let the script know it needs to execute this code block you need to use delayed expansion.

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: Why does %errorlevel% not work with an "if" statement?

#3 Post by MKANET » 14 Apr 2012 14:18

I already have it enabled.

(at the beginning of my script)

setlocal enableDelayedExpansion

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: Why does %errorlevel% not work with an "if" statement?

#4 Post by Fawers » 14 Apr 2012 14:21

MKANET wrote:

Code: Select all

if %BKfiledate%==%date% (
    find /c "<errors>0</errors>" %recentBK%
    if %errorlevel% equ 1 set today=failed
    echo %today%
)

Squashman wrote:Once you have it inside parenthesis to let the script know it needs to execute this code block you need to use delayed expansion.

Code: Select all

::added:
setlocal EnableDelayedExpansion
::original code with modifications:
if %BKfiledate%==%date% (
    find /c "<errors>0</errors>" %recentBK%
    if !errorlevel! equ 1 set today=failed
    echo !today!
)

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: Why does %errorlevel% not work with an "if" statement?

#5 Post by MKANET » 14 Apr 2012 14:24

Sorry, I guess you might have not seem my previous comment. I already have delayedexpansion enabled. I almost always enable it in my batch files.

Does my example actually work for you guys with delayedexpansion enabled? I would be very surprised if it did.

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: Why does %errorlevel% not work with an "if" statement?

#6 Post by MKANET » 14 Apr 2012 14:27

ohhhhhhhhhh... duh... I forgot to switch the % to !

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Why does %errorlevel% not work with an "if" statement?

#7 Post by Squashman » 14 Apr 2012 14:34

MKANET wrote:ohhhhhhhhhh... duh... I forgot to switch the % to !

BINGO!

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: Why does %errorlevel% not work with an "if" statement?

#8 Post by Fawers » 14 Apr 2012 14:37

MKANET wrote:ohhhhhhhhhh... duh... I forgot to switch the % to !

Heheh. Mistakes are common when writing codes.
We just take longer to realize what they are because, unlike C, Batch scripts are interpreted in real time; they don't point us exactly where an error occurs.

Post Reply