Home All Groups Group Topic Archive Search About

xcopy errorlevel problem in script

Author
7 Jan 2009 7:18 PM
Bond
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.


Here is my batch file:
--- begin cut ---
xcopy /E \\aml01\out$ \\ntbroadway\apps\aml
if not errorlevel 0 goto error
goto :EXIT

:error
echo error copying file...

Show quoteHide quote
:EXIT
echo exiting...
--- end cut ---

Author
7 Jan 2009 8:47 PM
Pegasus (MVP)
Show quote Hide quote
"Bond" <b***@james.com> wrote in message
news:u9CZ6yPcJHA.1188@TK2MSFTNGP05.phx.gbl...
> 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.
>
>
> Here is my batch file:
> --- begin cut ---
> xcopy /E \\aml01\out$ \\ntbroadway\apps\aml
> if not errorlevel 0 goto error
> goto :EXIT
>
> :error
> echo error copying file...
>
> :EXIT
> echo exiting...
> --- end cut ---

The statement
if not errorlevel 0 goto error
means: If the errorlevel is not 0 or not greater than 0 then goto error.
This is never the case! It should read instead:
if errorlevel 1 goto error
which means: If the error level is 1 or greater then goto error

A far more intuitive way to code such things in a batch file goes like this:
if %ErrorLevel% GTR 0 goto error
Note that GTR must be spelt in upper case. Type if /? at the Command Prompt
to see the available operators.

Note also that Microsoft has deprecated xcopy.exe. You should really use
robocopy.exe.
Author
7 Jan 2009 9:06 PM
Tom Lavedas
On Jan 7, 3:47 pm, "Pegasus \(MVP\)" <I.***@fly.com.oz> wrote:
> "Bond" <b***@james.com> wrote in message
>
> news:u9CZ6yPcJHA.1188@TK2MSFTNGP05.phx.gbl...
> {snip}

> Note that GTR must be spelt in upper case. Type if /? at the Command Prompt
> to see the available operators.

???  I don't believe that's true.  I just checked in XPSP2.  None of
the compare operators are case sensitive AFAIK.

Tom Lavedas
***********
http://there.is.no.more/tglbatch/
Author
7 Jan 2009 9:42 PM
Pegasus (MVP)
"Tom Lavedas" <tglba***@cox.net> wrote in message
news:f7f17456-f77b-4da4-8f92-55cce2f1b82c@z28g2000prd.googlegroups.com...
On Jan 7, 3:47 pm, "Pegasus \(MVP\)" <I.***@fly.com.oz> wrote:
> "Bond" <b***@james.com> wrote in message
>
> news:u9CZ6yPcJHA.1188@TK2MSFTNGP05.phx.gbl...
> {snip}

> Note that GTR must be spelt in upper case. Type if /? at the Command
> Prompt
> to see the available operators.

???  I don't believe that's true.  I just checked in XPSP2.  None of
the compare operators are case sensitive AFAIK.

Tom Lavedas
***********

Strange. I could have sworn this was the case. Must have had my head
elsewhere when I first looked at this issue several years ago. Thanks for
setting me right.
Author
7 Jan 2009 9:52 PM
Al Dunbar
Show quote Hide quote
"Pegasus (MVP)" <I.***@fly.com.oz> wrote in message
news:ujx0cDRcJHA.572@TK2MSFTNGP02.phx.gbl...
>
> "Tom Lavedas" <tglba***@cox.net> wrote in message
> news:f7f17456-f77b-4da4-8f92-55cce2f1b82c@z28g2000prd.googlegroups.com...
> On Jan 7, 3:47 pm, "Pegasus \(MVP\)" <I.***@fly.com.oz> wrote:
>> "Bond" <b***@james.com> wrote in message
>>
>> news:u9CZ6yPcJHA.1188@TK2MSFTNGP05.phx.gbl...
>> {snip}
>
>> Note that GTR must be spelt in upper case. Type if /? at the Command
>> Prompt
>> to see the available operators.
>
> ???  I don't believe that's true.  I just checked in XPSP2.  None of
> the compare operators are case sensitive AFAIK.
>
> Tom Lavedas
> ***********
>
> Strange. I could have sworn this was the case. Must have had my head
> elsewhere when I first looked at this issue several years ago. Thanks for
> setting me right.

IIRC, the earlier versions of NT based windows required these operators to
be given in upper case. Not an issue on XP (and likely Vista), and probably
w2k3.

/Al
Author
7 Jan 2009 10:02 PM
Bond
Show quote Hide quote
> The statement
> if not errorlevel 0 goto error
> means: If the errorlevel is not 0 or not greater than 0 then goto error.
> This is never the case! It should read instead:
> if errorlevel 1 goto error
> which means: If the error level is 1 or greater then goto error
>
> A far more intuitive way to code such things in a batch file goes like
> this:
> if %ErrorLevel% GTR 0 goto error
> Note that GTR must be spelt in upper case. Type if /? at the Command
> Prompt to see the available operators.
>
> Note also that Microsoft has deprecated xcopy.exe. You should really use
> robocopy.exe.

I've tried your suggestion as well as tried my error capture as `if
errorlevel 1 goto error' but either way, the errorlevel is never grreater
than 0 - no idea why...
Author
7 Jan 2009 10:06 PM
Al Dunbar
Show quote Hide quote
"Bond" <b***@james.com> wrote in message
news:u9CZ6yPcJHA.1188@TK2MSFTNGP05.phx.gbl...
> 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
     )

Of course, you may not know in advance the name of the file to be copied. If
you consider an empty folder to be an error, then I'd suggest you code your
script to test for that condition.


/Al

Show quoteHide quote
>
>
> Here is my batch file:
> --- begin cut ---
> xcopy /E \\aml01\out$ \\ntbroadway\apps\aml
> if not errorlevel 0 goto error
> goto :EXIT
>
> :error
> echo error copying file...
>
> :EXIT
> echo exiting...
> --- end cut ---
>
>
>
Author
7 Jan 2009 10:57 PM
Bond
Show quote Hide quote
"Al Dunbar" <aland***@hotmail.com> wrote in message
news:%23REq0QRcJHA.3856@TK2MSFTNGP06.phx.gbl...
>
> 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
>     )
>
> Of course, you may not know in advance the name of the file to be copied.
> If you consider an empty folder to be an error, then I'd suggest you code
> your script to test for that condition.

It appears that as long as I define a filename to be copied I recieve a
proper errorlevel with both your code and my code however, if I do not
define a filename it does not return a proper error code.  This code that
I've posted is just a small snippet of my script I am actually running.
Below is my actual production script that I'm trying to get to work.  The
xcopy process works when there are files/folders to copy but does not change
the errorlevel when there are zero files/folders.

REM %1 = source computer name

REM %2 = target computer name & path

REM %3 = source share name

REM %4 = source file Spec

if %1 == "" goto :error

if %2 == "" goto :error

if %3 == "" goto :error

if %4 == "" goto :error



:start

REM *******************************

REM First we check for the existance of a directory on the source using FOR

REM If one exists we xcopy it to the destination

REM IF not, we exit out with errorlevel 2

REM and try again after 2 hours for up to 10 times

REM *******************************

For /l %%x in (1,1,2) do (

            For /d %%i in (\\%1\%3\%4) do (

                        if exist %%i\*.xml goto :copy

            )

            choice /c YN /t 2 /d N /m "Do you want to stop processing of
this batch?"

)



REM ********************************

REM This is the xcopy process - the looping results of the FOR command

REM reads the directory names and pipes the names into the variable %a

REM Xcopy errorlevels 5=disk-write error, 4=initialization error, 3=n/a
2=xcopy terminated by CTRL-C, 1=no files found, 0=OK

REM ********************************

:copy

For /d %%a in (\\%1\%3) do xcopy /E /V /H /Y "%%a" \\%2

if errorlevel 5 goto error

if errorlevel 4 goto error

if errorlevel 2 goto error

if errorlevel 1 goto error



REM ********************************

REM Once the xcopy job is completed we remove the directories from the
source

REM ********************************

for /d %%a in (\\%1\%3\*) do echo rd /s /q "%%a"



REM ********************************

REM and any files that may be in the root of the source folder

REM ********************************

for %%a in (\\%1\%3\*) do echo del "%%a"

goto :EXIT



:error

@echo File copy error...

\\central01\gecs$\gecsret.exe 1





Show quoteHide quote
:exit
Author
8 Jan 2009 5:16 AM
Al Dunbar
Show quote Hide quote
"Bond" <b***@james.com> wrote in message
news:uZKFMtRcJHA.1188@TK2MSFTNGP05.phx.gbl...
>
> "Al Dunbar" <aland***@hotmail.com> wrote in message
> news:%23REq0QRcJHA.3856@TK2MSFTNGP06.phx.gbl...
>>
>> 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
>>     )
>>
>> Of course, you may not know in advance the name of the file to be copied.
>> If you consider an empty folder to be an error, then I'd suggest you code
>> your script to test for that condition.
>
> It appears that as long as I define a filename to be copied I recieve a
> proper errorlevel with both your code and my code however, if I do not
> define a filename it does not return a proper error code.

This is an incorrect interpretation. CMD *ALWAYS* returns a proper error
code, because it returns the error code that results from the operation -
from its point of view.

The fact that a folder might happen to be empty is not intrinsically an
error - that is certainly not the case when you create a directory with no
files in it - should MD always return a non-zero error code?

Further, a non-zero ERRORLEVEL value does not always indicate an error, or
even that anything has gone wrong. Consider the CHOICE command that uses the
ERRORLEVEL to indicate to the script which key was pressed...

>    This code that I've posted is just a small snippet of my script I am
> actually running. Below is my actual production script that I'm trying to
> get to work.  The xcopy process works when there are files/folders to copy
> but does not change the errorlevel when there are zero files/folders.

Again, I disagree. If your code assumes that "1=no files found", then the
error lies with the code.

As I tried to suggest previously, if you tell the XCOPY to copy all of the
files in a folder, it will successfully copy zero files if the folder is
empty. Certainly, there is no file that it failed to copy.

Batch scripting is somewhat unsophisticated in many ways, quirky in others.
You will continually be frustrated by expecting it to think in your terms.
This part of your script would appear to indicate you may not have fully
grasped what "IF ERRORLEVEL {number}" actually means:

    if errorlevel 5 goto error
    if errorlevel 4 goto error
    if errorlevel 2 goto error
    if errorlevel 1 goto error

as the above statements will have exactly the same result as this single
statement:

    if errorlevel 1 goto error


/Al
Author
8 Jan 2009 2:30 PM
Bond
"Al Dunbar" <aland***@hotmail.com> wrote in message
news:OcEP37YcJHA.3520@TK2MSFTNGP05.phx.gbl...
> This is an incorrect interpretation. CMD *ALWAYS* returns a proper error
> code, because it returns the error code that results from the operation -
> from its point of view.

Well, your point here is what it really comes down to.  I'm trying to get
xcopy to return something that I'm expecting and not what it interprets
hence my problem with getting this code to work.  Sigh...

So do you have any suggestions as to how I can actually do what I am trying
to do?

> You will continually be frustrated by expecting it to think in your terms.
> This part of your script would appear to indicate you may not have fully
> grasped what "IF ERRORLEVEL {number}" actually means:
>
>    if errorlevel 5 goto error
>    if errorlevel 4 goto error
>    if errorlevel 2 goto error
>    if errorlevel 1 goto error
>
> as the above statements will have exactly the same result as this single
> statement:
>

It appears that way only in an effort to catch any exit code xcopy returned.
This was added during troubleshooting.  I am only interested in errorlevel 1
Author
8 Jan 2009 6:44 PM
Al Dunbar
"Bond" <b***@james.com> wrote in message
news:OD67i2ZcJHA.1188@TK2MSFTNGP05.phx.gbl...
>
> "Al Dunbar" <aland***@hotmail.com> wrote in message
> news:OcEP37YcJHA.3520@TK2MSFTNGP05.phx.gbl...
>> This is an incorrect interpretation. CMD *ALWAYS* returns a proper error
>> code, because it returns the error code that results from the operation -
>> from its point of view.
>
> Well, your point here is what it really comes down to.  I'm trying to get
> xcopy to return something that I'm expecting and not what it interprets
> hence my problem with getting this code to work.  Sigh...

To paraphrase: you are trying to get xcopy to behave in a manner that seems
logical to you in the context of your script, but in a way that it will not
behave. And that way lies frustration...

> So do you have any suggestions as to how I can actually do what I am
> trying to do?

I had suggested earlier that you write a separate section of code that
checks to see if the folder is empty, as, apparently, in the context of what
you are doing, that is an error condition. This is another area where things
are not as intuitively obvious as they might be... Fortunately, the question
has been asked and answered by others elsewhere in these groups.

Alternately, you could trap the output of the xcopy command and look for "0
File(s) copied", however you;d likely come to the wrong conclusion if 10 or
20 files were copied...

Show quoteHide quote
>> You will continually be frustrated by expecting it to think in your
>> terms. This part of your script would appear to indicate you may not have
>> fully grasped what "IF ERRORLEVEL {number}" actually means:
>>
>>    if errorlevel 5 goto error
>>    if errorlevel 4 goto error
>>    if errorlevel 2 goto error
>>    if errorlevel 1 goto error
>>
>> as the above statements will have exactly the same result as this single
>> statement:
>>
>
> It appears that way only in an effort to catch any exit code xcopy
> returned. This was added during troubleshooting.  I am only interested in
> errorlevel 1

Yes, I understand that. But do *you* understand that if the errorlevel is
set to 1, the if errorlevel 5 goto error will cause control to transfer to
the error label?

/Al
Author
7 Jan 2009 11:01 PM
Pegasus (MVP)
Show quote Hide quote
"Al Dunbar" <aland***@hotmail.com> wrote in message
news:%23REq0QRcJHA.3856@TK2MSFTNGP06.phx.gbl...
>
> "Bond" <b***@james.com> wrote in message
> news:u9CZ6yPcJHA.1188@TK2MSFTNGP05.phx.gbl...
>> 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. I see two
potential problems:
- You're comparing pears with apples
- You're assuming that the "echo" command leaves the errorlevel of the
preceding command untouched.

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. IMHO, one should not rely on this: Errorlevels should be
tested immediately after the command that generates them.
Author
8 Jan 2009 4:47 AM
Al Dunbar
Show quote Hide quote
"Pegasus (MVP)" <I.***@fly.com.oz> wrote in message
news:udzwjvRcJHA.1760@TK2MSFTNGP05.phx.gbl...
>
> "Al Dunbar" <aland***@hotmail.com> wrote in message
> news:%23REq0QRcJHA.3856@TK2MSFTNGP06.phx.gbl...
>>
>> "Bond" <b***@james.com> wrote in message
>> news:u9CZ6yPcJHA.1188@TK2MSFTNGP05.phx.gbl...
>>> 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
Author
8 Jan 2009 2:25 PM
Pegasus (MVP)
"Al Dunbar" <aland***@hotmail.com> wrote in message
news:OLNos7YcJHA.3520@TK2MSFTNGP05.phx.gbl...
<snip>
>> 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:

Agreed. My objection relates to the mix of methods while testing the script.

<snip>

>>    I see two potential problems:
>> - You're comparing pears with apples
>
> which is the pear and which is the apple?

pear = errorlevel
apple=%errorlevel%

Show quoteHide quote
>> - 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%

You can never demonstrate the falsity of a claim unless you have
performed an exhaustive test of all possible combinations, which
is clearly impossible. You appear to have tested a single batch file.
Here is one that will substantiate the claim. You must run it both as
a .bat and as a .cmd file:
@echo off
rem Demonstrating that the following commands will set an errorlevel
rem in .cmd files but not in .bat files:
rem path, append, prompt, set, assoc
echo Executing %0
rem Generate an error
find 2>nul
echo ErrorLevel=%errorlevel%
set test=xyz
echo ErrorLevel=%errorlevel%
Author
9 Jan 2009 5:47 AM
Al Dunbar
Show quote Hide quote
"Pegasus (MVP)" <I.***@fly.com.oz> wrote in message
news:OFpXszZcJHA.1916@TK2MSFTNGP02.phx.gbl...
>
> "Al Dunbar" <aland***@hotmail.com> wrote in message
> news:OLNos7YcJHA.3520@TK2MSFTNGP05.phx.gbl...
> <snip>
>>> 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:
>
> Agreed. My objection relates to the mix of methods while testing the
> script.

I thought so, however, one could use "if errorlevel" in a script in which
one also in some cases needed to examine the variable's actual value...

> <snip>
>
>>>    I see two potential problems:
>>> - You're comparing pears with apples
>>
>> which is the pear and which is the apple?
>
> pear = errorlevel
> apple=%errorlevel%

I thought so. Fortunately, those two fruit never once appeared in the same
statement, let alone in a context in which they were compared.

Show quoteHide quote
>>> - 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%
>
> You can never demonstrate the falsity of a claim unless you have
> performed an exhaustive test of all possible combinations, which
> is clearly impossible. You appear to have tested a single batch file.
> Here is one that will substantiate the claim. You must run it both as
> a .bat and as a .cmd file:

I did do that, and said so: ... by running this test script as "a .bat and
as a .cmd file"

> @echo off
> rem Demonstrating that the following commands will set an errorlevel
> rem in .cmd files but not in .bat files:
> rem path, append, prompt, set, assoc
> echo Executing %0
> rem Generate an error
> find 2>nul
> echo ErrorLevel=%errorlevel%
> set test=xyz
> echo ErrorLevel=%errorlevel%

Interesting. Have you ever seen an explanation for this behaviour?

Note that it was an ECHO command (not a SET command) that I inserted
between the command setting the error code and the "if errorlevel"
statement, and my test demonstrated that the echo command does not reset
errorlevel in either type of batch file.

But, while we are being picky, I see that you use the ECHO command without a
non-blank separator. This, too, is poor practice as shown here:

    @echo off

    echo/
    echo/have a look at these six words:
    echo/one
    echo/two
    echo/on
    echo/under
    echo/over
    echo/off
    echo/

    echo/now try to see them here:
    echo one
    echo two
    echo on
    echo under
    echo over
    echo off
    echo

    pause

Oh, but wait, you supplied that script simply to highlight a specific point,
not as an example of a robust script ;-)

/Al
Author
9 Jan 2009 6:10 AM
Pegasus (MVP)
"Al Dunbar" <aland***@hotmail.com> wrote in message
news:OblFS3hcJHA.2444@TK2MSFTNGP06.phx.gbl...
<snip>
> But, while we are being picky, I see that you use the ECHO command without
> a
> non-blank separator. This, too, is poor practice as shown here:
>

Which line were you thinking off?
Author
9 Jan 2009 1:02 PM
Al Dunbar
"Pegasus (MVP)" <I.***@fly.com.oz> wrote in message
news:Opkj3DicJHA.1704@TK2MSFTNGP05.phx.gbl...
>
> "Al Dunbar" <aland***@hotmail.com> wrote in message
> news:OblFS3hcJHA.2444@TK2MSFTNGP06.phx.gbl...
> <snip>
>> But, while we are being picky, I see that you use the ECHO command
>> without a
>> non-blank separator. This, too, is poor practice as shown here:
>>
>
> Which line were you thinking off?

Oh, none of the lines in your code would have suffered from the problem.
But, in the same way you contend that it is poor practice to use different
types of ERRORLEVEL tests in a single batch script, and to assume that
intervening commands will not alter the ERRORLEVEL code, I contend that one
should ALWAYS use "ECHO/" rather than "ECHO/" because this will make one
less likely to make the mistakes illustrated by the example script I gave.

hint: I'm just throwing your pickiness back at you (for fun, of course)

/Al
Author
9 Jan 2009 8:28 PM
Pegasus (MVP)
Show quote Hide quote
"Al Dunbar" <aland***@hotmail.com> wrote in message
news:%23Ytn8plcJHA.5888@TK2MSFTNGP02.phx.gbl...
>
> "Pegasus (MVP)" <I.***@fly.com.oz> wrote in message
> news:Opkj3DicJHA.1704@TK2MSFTNGP05.phx.gbl...
>>
>> "Al Dunbar" <aland***@hotmail.com> wrote in message
>> news:OblFS3hcJHA.2444@TK2MSFTNGP06.phx.gbl...
>> <snip>
>>> But, while we are being picky, I see that you use the ECHO command
>>> without a
>>> non-blank separator. This, too, is poor practice as shown here:
>>>
>>
>> Which line were you thinking off?
>
> Oh, none of the lines in your code would have suffered from the problem.
> But, in the same way you contend that it is poor practice to use different
> types of ERRORLEVEL tests in a single batch script, and to assume that
> intervening commands will not alter the ERRORLEVEL code, I contend that
> one should ALWAYS use "ECHO/" rather than "ECHO/" because this will make
> one less likely to make the mistakes illustrated by the example script I
> gave.
>
> hint: I'm just throwing your pickiness back at you (for fun, of course)
>
> /Al

It seems that we agree by and large, although perhaps not on all the
details.

In your previous reply you asked "Interesting. Have you ever seen an
explanation for this behaviour?", while referring to the setting/not setting
of errorlevels. No, I haven't seen any explanation - I wasn't even aware
until recently that there is a difference between .bat and .cmd files. I
always assumed that the Command Processor would use the same interpretive
code for the two file types.

IMHO, the method used for .cmd files is more consistent than the method used
for .bat files. I like to think that the errorlevel should be set/reset by
each and every command in a batch file, whether it be for find.exe, echo,
set or append.exe. As it happens, the commands "path", "append", "prompt",
"set" and "assoc" (and perhaps others) won't affect the pre-existing error
level in a .bat file. Weird.
Author
7 Jan 2009 11:03 PM
Bond
"Al Dunbar" <aland***@hotmail.com> wrote in message
news:%23REq0QRcJHA.3856@TK2MSFTNGP06.phx.gbl...
> Of course, you may not know in advance the name of the file to be copied.
> If you consider an empty folder to be an error, then I'd suggest you code
> your script to test for that condition.


Sorry, that last paste didn't work out so well...