Windows Server - xcopy errorlevel problem in script

Asked By Bond
07-Jan-09 02:18 PM
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

echo error copying file...

echo exiting...
--- end cut ---
Vista
(1)
XP
(1)
Unneccessary
(1)
Directories
(1)
Screenshots
(1)
Errorlevel
(1)
Pickiness
(1)
Directory
(1)
  Pegasus \(MVP\) replied...
07-Jan-09 03:47 PM
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.
  Tom Lavedas replied...
10-Jan-09 02:05 AM
pt

???  I do not believe that is true.  I just checked in XPSP2.  None of
the compare operators are case sensitive AFAIK.

Tom Lavedas
***********
http://there.is.no.more/tglbatch/
  Pegasus \(MVP\) replied...
07-Jan-09 04:42 PM
???  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.
  Al Dunbar replied...
07-Jan-09 04:52 PM
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
  Bond replied...
07-Jan-09 05:02 PM
I have 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...
  Al Dunbar replied...
07-Jan-09 05:06 PM
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
  Bond replied...
07-Jan-09 05:57 PM
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




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


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




@echo File copy error...

\\central01\gecs$\gecsret.exe 1
  Pegasus \(MVP\) replied...
07-Jan-09 06:01 PM
Allow me to cringe. Testing for %errorlevel% on one line and processing
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.
  Bond replied...
07-Jan-09 06:03 PM
Sorry, that last paste did not work out so well...
  Al Dunbar replied...
07-Jan-09 11:47 PM
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...


which is the pear and which is the apple?


I would never do this in operational code because of the potential ambiguity
you point out, but...


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%


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
  Al Dunbar replied...
08-Jan-09 12:16 AM
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...


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
  Pegasus \(MVP\) replied...
08-Jan-09 09:25 AM
Agreed. My objection relates to the mix of methods while testing the script.



pear = errorlevel
apple=%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%
  Bond replied...
08-Jan-09 09:30 AM
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?


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
  Al Dunbar replied...
08-Jan-09 01:44 PM
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...


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


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
  Al Dunbar replied...
09-Jan-09 12:47 AM
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...


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


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


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
  Pegasus \(MVP\) replied...
09-Jan-09 01:10 AM
Which line were you thinking off?
  Al Dunbar replied...
09-Jan-09 08:02 AM
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
  Pegasus \(MVP\) replied...
09-Jan-09 03:28 PM
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",
level in a .bat file. Weird.
help
Windows Server Drive map problem SBS 2008 and Vista Hi I have 2 shared drives mapped using Group Policy Preference. The GP is applied to the whole domain. This works fine on all our XP machines. I have now setup a Vista (SP1) client, installed the windowsupdate for group policy extension. Things like printers from group policy 8859-1" Content-Transfer-Encoding: quoted-printable have you run gpupdate / force on server and Vista machines? - - = 20 Cris Hanna [SBS - MVP] Co-Contributor, Windows Small Business Server 2008 Unleashed http Preference. The GP is applied to the whole domain. This works fine on all our XP machines. I have now setup a Vista (SP1) client, installed the windowsupdate for group policy extension. Things like printers from group policy html; charset = "iso-8859-1" Content-Transfer-Encoding: quoted-printable charset = 3Diso-8859-1"> and Vista = 20 machines?< / FONT> < / DIV> Business = 20 Server 2008 Unleashed<BR> <A = 20 href = 3D"http is = 20 applied to the whole domain.<BR> <BR> This works fine on all our XP = 20 machines.<BR> <BR> I have now setup a Vista (SP1) client, installed the = 20
Windows Server Join SBS Domain - 64 bit Vista Client I am in the throes of setting-up an SBS 2008 server in my adding computers to the new domain one at a time. I have successfully added an XP Pro workstation via the http: / / connect approach which is working as expected and I've user logins. No problems. I am now attempting to add a client running 64 bit Vista with no luck. During the process, it hangs, consistently at the same point and collects configuration. Same results. All computers use DHCP which is provided by the server. Both the XP and Vista machines were full members / participants of my SBS 2003 domain before I removed them to the Sharepoint stuff is working as evidenced by the fact that I could join the XP machine via the http: / / connect routine as well as access all remote functions, including Companyweb, Remote Web Workplace, OWA, etc. There is nothing out of the ordinary with the Vista machine. I have attempted the connection routing with the Windows firewall both enabled and disabled 5 software doesn't appear to be a problem as that is running on the XP machine as well. This is my only "packmule" Vista machine. I'm hoping to get
Windows Server SBS2000 Fax Client and Vista Hello everyone, Are you able to help? We have a customer running SBS2000 and have recently updated some of the client PC's to Vista Business and Ultimate. All was working well until they needed to use the Shared Fax what is put in the field? Is there a soultion to this problem or is Vista not compatible with eariler Microsoft products? Many Thanks Andy Millennium Business Systems This is a text / plain; charset = "Utf-8" Content-Transfer-Encoding: quoted-printable compatibility patches were released for Vista to run with SBS 2003, but = even there. . .it requires some manual configuration don't customer running SBS2000 and have = recently = 20 updated some of the client PC's to Vista Business and Ultimate. All = was = 20 working well until they needed to use the Shared is put in the field? Is = there a = 20 soultion to this problem or is Vista not compatible with eariler = Microsoft = 20 products? Many Thanks Andy Millennium Business Systems - -- -- - = _NextPart_000_0095_01C890E8.13F2F0C0 quoted-printable = EF = BB = BF<!DOCTYPE HTML PUBLIC "- / / W3C / / DTD HTML 4.0 Transitional / / EN"> Vista to = 20 run with SBS 2003, but even there. . .it requires some manual = 20 configuration running = 20 SBS2000 and have recently <BR> updated some of the client PC's to Vista = Business and Ultimate. All was <BR> working well until they needed to = use the = 20
Windows Server Windows Server 2003 SP2 and Vista Issue. Hi all, I have installed Windows Server 2003 SP2 on my R2 server. Now when I try and browse the shares of the server from Windows Vista (I have two installs of Vista) it is very slow to resolve folder names. For example I have a drive mapped folder I try to open inside any share. If I access the server using Windows XP, no problems at all. Even running a virtual machine with XP on a Vista host has no problems. I am reluctant to uninstall the SP2 because of security, but NBT. 2) Changed the local Group Policy to disable all SMB signing. 3) Connected to Vista from the Server, no delay when browsing Vista from the Server. 4) Installed Network Monitor v3 on Vista and had a look at