CMD return code issue

tesfaye

2[H]4U
Joined
Apr 17, 2003
Messages
3,421
Has anyone out there run into this problem:

You run a program from within an If statement and try to evaluate the return code only to find it is set to 0 as in the following example:

IF exists "\\%FileServer%\sys\users\%username%\Outlook\%file%" (
<...do stuff...>
IFMEMEBER "%UserDomain%\Circular230" <---For the user running the test this finds a match and displays a message to that effect and under normal circumstances sets ERRORLEVEL = 1
echo %ERRORLEVEL% <---This should output 1 but it outputs 0 instead.
pause
IF [%ERRORLEVEL%] == [1] (
%systemroot%\system32\cscript.exe <script> //Nologo
)
Goto:EOF
)

To fix it I did the following:

IF exists "\\%FileServer%\sys\users\%username%\Outlook\%file%" (
Xcopy ...
Goto SETREG
)

<..other stuff..>

:SETREG
IFMEMEBER "%UserDomain%\Circular230"
echo %ERRORLEVEL% <---This now outputs 1
pause
IF [%ERRORLEVEL%] == [1] (
%systemroot%\system32\cscript.exe <script> //Nologo
)
Goto:EOF

It seems like the code for handling IF doesn't handle return codes or am I missing something? I normally do things in VBScript but for quick and dirty solutions I use batch. Shame on me?

The script now does what it's supposed to do but I just wanted an explanation for this behavior or a tip on how to properly handle return codes when inside an IF statement.

Thanks in advance.
 
Back
Top