Windows Vista Tips

Windows Vista Tips > Newsgroups > Windows Server > Scripting > xcopy errorlevel problem in script

Reply
Thread Tools Display Modes

xcopy errorlevel problem in script

 
 
Bond
Guest
Posts: n/a

 
      01-07-2009
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 ---



 
Reply With Quote
 
 
 
 
Pegasus \(MVP\)
Guest
Posts: n/a

 
      01-07-2009

"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.
>
>
> 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.


 
Reply With Quote
 
Pegasus \(MVP\)
Guest
Posts: n/a

 
      01-07-2009

"Tom Lavedas" <> wrote in message
news:f7f17456-f77b-4da4-8f92-...
On Jan 7, 3:47 pm, "Pegasus \(MVP\)" <I....@fly.com.oz> wrote:
> "Bond" <b...@james.com> wrote in message
>
> news:...
> {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.


 
Reply With Quote
 
Al Dunbar
Guest
Posts: n/a

 
      01-07-2009

"Pegasus (MVP)" <> wrote in message
news:...
>
> "Tom Lavedas" <> wrote in message
> news:f7f17456-f77b-4da4-8f92-...
> On Jan 7, 3:47 pm, "Pegasus \(MVP\)" <I....@fly.com.oz> wrote:
>> "Bond" <b...@james.com> wrote in message
>>
>> news:...
>> {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


 
Reply With Quote
 
Bond
Guest
Posts: n/a

 
      01-07-2009

> 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...


 
Reply With Quote
 
Al Dunbar
Guest
Posts: n/a

 
      01-07-2009

"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
)

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

>
>
> 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 ---
>
>
>



 
Reply With Quote
 
Bond
Guest
Posts: n/a

 
      01-07-2009

"Al Dunbar" <> wrote in message
news:%...
>
> 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





:exit





 
Reply With Quote
 
Pegasus \(MVP\)
Guest
Posts: n/a

 
      01-07-2009

"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. 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.


 
Reply With Quote
 
Bond
Guest
Posts: n/a

 
      01-07-2009

"Al Dunbar" <> wrote in message
news:%...
> 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...


 
Reply With Quote
 
Al Dunbar
Guest
Posts: n/a

 
      01-08-2009

"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


 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RE: XCopy in login script mtstream Windows Server 0 02-05-2007 10:31 PM
Speeding up XCOPY script-possible?? Courtney R Scripting 2 12-22-2005 03:51 AM
XCOPY Script Error Ben Windows Server 6 10-29-2005 04:36 AM
xCopy Script Error Mike Scripting 1 06-27-2005 10:07 AM
Xcopy script with a GUI prompt Serge Ayotte Scripting 2 02-11-2005 02:15 PM



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59