%errorlevel% doesn't work in WindowsXP.
As I know when an error occurs %errorlevel% set 1 or above value, and if there is no error it will set 0.
But, even though there is no error, %errorlevel% is 1. And I set %errorlevel% 0, even there is an error %errorlevel% is sill 0.
I think OS doesn't change %errorlevel% in XP.
In Win7 it work totally.
@echo off
setlocal enabledelayedexpansion
call dir
echo errorlevel=%errorlevel%
REM expected errorlevel=0 but 1
call dor
echo errorlevel=%errorlevel%
REM expected errorlevel=1
set errorlevel=0
call dor
echo errorlevel=%errorlevel%
REM expected errorlevel=1 but 0
But if errorlevel 1 () looks work.
some examples for you:
@ECHO OFF &SETLOCAL
echo errorlevel=%errorlevel%
call dir >nul
echo errorlevel=%errorlevel%
output:
errorlevel=0
errorlevel=0
@ECHO OFF &SETLOCAL
echo errorlevel=%errorlevel%
call dor
echo errorlevel=%errorlevel%
output:
errorlevel=0
'dor' is not recognized as an internal or external command,
operable program or batch file.
errorlevel=1
@ECHO OFF &SETLOCAL
::set errorlevel=0 illegal operation!
set test=1
set test
echo errorlevel=%errorlevel%
call dor
echo errorlevel=%errorlevel%
output:
test=1
errorlevel=0
'dor' is not recognized as an internal or external command,
operable program or batch file.
errorlevel=1
set errorlevel
is not a legal command. If you need to set the errorlevel system environment variable, do this with a usual command.
@ECHO OFF &SETLOCAL
::set errorlevel=1 illegal operation!
set test
echo errorlevel=%errorlevel%
call dir >nul
echo errorlevel=%errorlevel%
output:
Environment variable test not defined
errorlevel=1
errorlevel=0