"Pegasus (MVP)" <> wrote in message
news:...
>
> "Al Dunbar" <> wrote in message
> news:%...
>>
>> "Bond" <> wrote in message
>> news:...
>>> Please forgive me if this question is not appropriate to post here, I've
>>> exhausted all my research and troubleshooting on this silly process so I
>>> hope you don't mind me posting this question here. I need to copy some
>>> files from a W2K3 server to another server using XCOPY. It is a simple
>>> batch file which uses an errorlevel to evaluate whether the file(s)
>>> copied or not. What is not working is the errorlevel check - no mater
>>> if the batch file copies files or not, I can not get xcopy to return an
>>> errorlevel greater than 0.
>>>
>>> The batch file is executed on machine name "SERVER1"
>>>
>>> Here are a couple screenshots of my results.
>>> --- begin cut (should return errorlevel 0 example)---
>>> C:\batch>amlmove1.bat
>>> C:\batch>xcopy /E \\aml01\out$ \\backup01\apps\aml
>>> \\aml01\out$\MASTER_25Sep2008.xml
>>> 1 File(s) copied
>>> C:\batch>if not errorlevel 0 goto error
>>> C:\batch>goto :EXIT
>>> C:\batch>echo exiting...
>>> exiting...
>>> --- end cut ---
>>>
>>>
>>> Now I remove the file from \\aml01\out$ and run the batch file again
>>> --- begin cut (should return errorlevel 1 example) ---
>>> C:\batch>amlmove1.bat
>>> C:\batch>xcopy /E \\aml01\out$ \\backup01\apps\aml
>>> 0 File(s) copied
>>> C:\batch>if not errorlevel 0 goto error
>>> C:\batch>goto :EXIT
>>> C:\batch>echo exiting...
>>> exiting...
>>> --- end cut ---
>>> Note that in the above example it should have returned "error copying
>>> file..." rather than "exiting..." I have also added a `echo
>>> %errorlevel% just before the if statement and each time a "0" is
>>> returned.
>>
>> The ERRORLEVEL test is a red herring, here. When you XCOPY all of the
>> files from a folder (or share), this will succeed regardless of the
>> number of files, whether that be one file, a thousand files - or zero
>> files.
>>
>> Try running this version of your script before and after you delete that
>> file:
>>
>> xcopy /E \\aml01\out$\MASTER_25Sep2008.xml \\backup01\apps\aml\
>> echo/errorlevel is %errorlevel%
>> if errorlevel 1 (
>> echo/error occurred
>> ) else (
>> echo/xcopy succeeded
>> )
>>
>
> Allow me to cringe. Testing for %errorlevel% on one line and processing
> "errorlevel" (note the absence of the % chars) is not nice.
Cringe away. But there is only one *test* for the error level. The echo is
there simply to display its numerical value, as the OP did much the same in
his testing.
There is nothing intrinsically "not nice" about using the old style test:
if errorlevel 1 ...
instead of the newer:
if %errorlevel% gtr 0 ...
Although newbies often misunderstand the original, the newer way is not
without its own pitfalls like:
if "%errorlevel%" gtr 5 ...
Another place I avoid unneccessary percent signs is here:
set/a area = length * width
which does a better job of reminding one that the /a switch makes the set
command behave in a radically different manner. But I digress...
> I see two potential problems:
> - You're comparing pears with apples
which is the pear and which is the apple?
> - You're assuming that the "echo" command leaves the errorlevel of the
> preceding command untouched.
I would never do this in operational code because of the potential ambiguity
you point out, but...
>
> The other day I came across an obscure reference that demonstrated that
> certain commands such as "echo" leave errorlevels intact in .bat files but
> not in .cmd files.
I'm not sure of the validity of an "obscure reference" when the falsity of
the statement can be demonstrated by running a .bat and a .cmd file
containing the following code:
@echo off
dir no-such-file
echo/%errorlevel%
echo/%errorlevel%
> IMHO, one should not rely on this: Errorlevels should be tested
> immediately after the command that generates them.
Yes, I do agree with this statement. Not because ECHO might sometimes
misbehave, but because one might inadvertently add something else before the
test that actually does change the error code.
/Al
|