Xcopy behavior questions

S

Stephen Quist

Hi all,

I'm using an xcopy command line in a batch file that looks like this,
xcopy /R /C /E /Y /D /z /exclude:i:\netdir\excludelist.txt i:\netdir %1


to distribute updated files to local machines.

I'd like to test the result of the xcopy to see if any files were copied.

The help indicated that after the command errorlevel should be 0 if the

files were copied without error and 1 if no files were found to copy.

What I find is that the return value is always 0, even when no files

were actually copied! This does not seem like expected behavior to me.

Am I doing something wrong here?



Also, tell me if I'm remembering a dream. I thought there was a way

to run xcopy in a test mode where it would set the return code according

to the result of running the command, but no actual files would be copied.

Does this sound familiar to anyone?



Thanks,

Steve
 
M

Matthias Tacke

Stephen Quist said:
Hi all,

I'm using an xcopy command line in a batch file that looks like this,
xcopy /R /C /E /Y /D /z /exclude:i:\netdir\excludelist.txt i:\netdir %1


to distribute updated files to local machines.

I'd like to test the result of the xcopy to see if any files were copied.

The help indicated that after the command errorlevel should be 0 if the

files were copied without error and 1 if no files were found to copy.

What I find is that the return value is always 0, even when no files

were actually copied! This does not seem like expected behavior to me.

Am I doing something wrong here?



Also, tell me if I'm remembering a dream. I thought there was a way

to run xcopy in a test mode where it would set the return code according

to the result of running the command, but no actual files would be copied.

Does this sound familiar to anyone?
Sounds you're talking/dreaming of xxcopy ?
www.xxcopy.com
HTH
 
W

wadester

Hi all,

I'm using an xcopy command line in a batch file that looks like this,
xcopy /R /C /E /Y /D /z /exclude:i:\netdir\excludelist.txt i:\netdir %1

to distribute updated files to local machines.
I'd like to test the result of the xcopy to see if any files were copied.
The help indicated that after the command errorlevel should be 0 if the
files were copied without error and 1 if no files were found to copy.
What I find is that the return value is always 0, even when no files
were actually copied! This does not seem like expected behavior to me.
Am I doing something wrong here?

I can't figure out how to get an ERRORLEVEL 1 either.
Also, tell me if I'm remembering a dream. I thought there was a way
to run xcopy in a test mode where it would set the return code according
to the result of running the command, but no actual files would be copied.
Does this sound familiar to anyone?

Use XCOPY /L.

ws
 
S

Stephen Quist

wadester wrote:
:: In article <[email protected]>,
:: (e-mail address removed)-co.com wrote...
::: Hi all,
:::
::: I'm using an xcopy command line in a batch file that looks like
::: this, xcopy /R /C /E /Y /D /z /exclude:i:\netdir\excludelist.txt
::: i:\netdir %1
:::
::: to distribute updated files to local machines.
::: I'd like to test the result of the xcopy to see if any files were
::: copied. The help indicated that after the command errorlevel should
::: be 0 if the files were copied without error and 1 if no files were
::: found to copy. What I find is that the return value is always 0,
::: even when no files were actually copied! This does not seem like
::: expected behavior to me. Am I doing something wrong here?
::
:: I can't figure out how to get an ERRORLEVEL 1 either.
::
::: Also, tell me if I'm remembering a dream. I thought there was a way
::: to run xcopy in a test mode where it would set the return code
::: according to the result of running the command, but no actual files
::: would be copied. Does this sound familiar to anyone?
::
:: Use XCOPY /L.
::
Thanks! It's right there in the help and for some reason I just didn't see
it. <ugh>

I still can't get errorleve 1either.

Steve
 
S

Stephen Quist

Major Ninth wrote:
:: did you use ERRORLEVEL or %ERRORLEVEL% ?
:: -john
::
I used the standard syntax. I've tried it with nonexistent directories
and empty directories. None of my tests gave me an errorlevel of 1.
Here's my test file:

@setlocal
if %1. == . goto needdir
@if exist i:\not_now.text goto notnow

xcopy i:\esprel %1 /r /c /l /d /exclude:i:\esprel\excludelist.txt >temp.txt

@if errorlevel 5 goto e5
@if errorlevel 4 goto e4
@if errorlevel 2 goto e2
@if errorlevel 1 goto e1
@if errorlevel 0 goto e0
:e5
@echo errorlevel 5
@goto end
:e4
@echo errorlevel 4
@goto end
:e2
@echo errorlevel 2
@goto end
:e1
@echo errorlevel 1
@goto end
:e0
@echo errorlevel 0
@goto end


:needdir
@echo off
@echo test message
@goto end

:notnow
@type i:\not_now.text

:end
 
A

Al Dunbar [MS-MVP]

Stephen Quist said:
Major Ninth wrote:
:: did you use ERRORLEVEL or %ERRORLEVEL% ?
:: -john
::
I used the standard syntax.

Seems like standard DOS-based batch syntax - see suggestions below...
I've tried it with nonexistent directories
and empty directories. None of my tests gave me an errorlevel of 1.
Here's my test file:

@setlocal
if %1. == . goto needdir

or:

if "%1" EQU "" goto:needdir
@if exist i:\not_now.text goto notnow

xcopy i:\esprel %1 /r /c /l /d /exclude:i:\esprel\excludelist.txt
temp.txt

or replace these lines:
@if errorlevel 5 goto e5
@if errorlevel 4 goto e4
@if errorlevel 2 goto e2
@if errorlevel 1 goto e1
@if errorlevel 0 goto e0
:e5
@echo errorlevel 5
@goto end
:e4
@echo errorlevel 4
@goto end
:e2
@echo errorlevel 2
@goto end
:e1
@echo errorlevel 1
@goto end
:e0
@echo errorlevel 0
@goto end

with these lines:

@echo errorlevel is %errorlevel%
@goto:eof
:needdir
@echo off
@echo test message
@goto end

:notnow
@type i:\not_now.text

:end

Don't need a :end label, as :eof is implicitly there.

/Al
 
S

Stephen Quist

Thanks for the suggestions.

Is it true that the %errorlevel% usage is an NT improvement that is not
available under Win98?

Apparently, despite the clear statement in the help for Xcopy,
it never, under any circumstances, returns an errorlevel of 1.
Pity. I, for one, would find it useful.

Steve



Al Dunbar [MS-MVP] wrote:
:: :::
::: Major Ninth wrote:
::::: did you use ERRORLEVEL or %ERRORLEVEL% ?
::::: -john
:::::
::: I used the standard syntax.
::
:: Seems like standard DOS-based batch syntax - see suggestions below...
::
::: I've tried it with nonexistent directories
::: and empty directories. None of my tests gave me an errorlevel of 1.
::: Here's my test file:
:::
::: @setlocal
::: if %1. == . goto needdir
::
:: or:
::
:: if "%1" EQU "" goto:needdir
::
::: @if exist i:\not_now.text goto notnow
:::
::: xcopy i:\esprel %1 /r /c /l /d /exclude:i:\esprel\excludelist.txt
::: temp.txt
::
:: or replace these lines:
::
::: @if errorlevel 5 goto e5
::: @if errorlevel 4 goto e4
::: @if errorlevel 2 goto e2
::: @if errorlevel 1 goto e1
::: @if errorlevel 0 goto e0
:::: e5
::: @echo errorlevel 5
::: @goto end
:::: e4
::: @echo errorlevel 4
::: @goto end
:::: e2
::: @echo errorlevel 2
::: @goto end
:::: e1
::: @echo errorlevel 1
::: @goto end
:::: e0
::: @echo errorlevel 0
::: @goto end
::
:: with these lines:
::
:: @echo errorlevel is %errorlevel%
:: @goto:eof
::
:::
:::
:::: needdir
::: @echo off
::: @echo test message
::: @goto end
:::
:::: notnow
::: @type i:\not_now.text
:::
:::: end
::
:: Don't need a :end label, as :eof is implicitly there.
::
:: /Al
 
D

David Candy

No. It's always been available. The if statement is more powerful in NT but errorlevel is exactly the same.
 
M

Michael D. Ober

I used %errorlevel% in MS-DOS 5. You have to check your errorlevels in
descending order as the example below shows. That said, there may be a bug
in XCOPY. Also, MS never really documented the error levels returned by
their utilities.

Mike Ober.
 
D

David Candy

The following list shows each exit code and a brief description of its
meaning:

0
Files were copied without error.

1
No files were found to copy.

2
The user pressed CTRL+C to terminate XCOPY.

4
Initialization error occurred. There is not enough memory or disk space,
or you entered an invalid drive name or invalid syntax on the command
line.

5
Disk write error occurred.

from Dos 6.22 help file which is the best reference for exit codes.
--
----------------------------------------------------------
http://www.g2mil.com/Dec2003.htm
Michael D. Ober said:
I used %errorlevel% in MS-DOS 5. You have to check your errorlevels in
descending order as the example below shows. That said, there may be a bug
in XCOPY. Also, MS never really documented the error levels returned by
their utilities.

Mike Ober.

Stephen Quist said:
Thanks for the suggestions.

Is it true that the %errorlevel% usage is an NT improvement that is not
available under Win98?

Apparently, despite the clear statement in the help for Xcopy,
it never, under any circumstances, returns an errorlevel of 1.
Pity. I, for one, would find it useful.

Steve



Al Dunbar [MS-MVP] wrote:
:: :::
::: Major Ninth wrote:
::::: did you use ERRORLEVEL or %ERRORLEVEL% ?
::::: -john
:::::
::: I used the standard syntax.
::
:: Seems like standard DOS-based batch syntax - see suggestions below...
::
::: I've tried it with nonexistent directories
::: and empty directories. None of my tests gave me an errorlevel of 1.
::: Here's my test file:
:::
::: @setlocal
::: if %1. == . goto needdir
::
:: or:
::
:: if "%1" EQU "" goto:needdir
::
::: @if exist i:\not_now.text goto notnow
:::
::: xcopy i:\esprel %1 /r /c /l /d /exclude:i:\esprel\excludelist.txt
::: temp.txt
::
:: or replace these lines:
::
::: @if errorlevel 5 goto e5
::: @if errorlevel 4 goto e4
::: @if errorlevel 2 goto e2
::: @if errorlevel 1 goto e1
::: @if errorlevel 0 goto e0
:::: e5
::: @echo errorlevel 5
::: @goto end
:::: e4
::: @echo errorlevel 4
::: @goto end
:::: e2
::: @echo errorlevel 2
::: @goto end
:::: e1
::: @echo errorlevel 1
::: @goto end
:::: e0
::: @echo errorlevel 0
::: @goto end
::
:: with these lines:
::
:: @echo errorlevel is %errorlevel%
:: @goto:eof
::
:::
:::
:::: needdir
::: @echo off
::: @echo test message
::: @goto end
:::
:::: notnow
::: @type i:\not_now.text
:::
:::: end
::
:: Don't need a :end label, as :eof is implicitly there.
::
:: /Al
 
W

wadester

The following list shows each exit code and a brief description of its
meaning:

0
Files were copied without error.

1
No files were found to copy.

2
The user pressed CTRL+C to terminate XCOPY.

4
Initialization error occurred. There is not enough memory or disk space,
or you entered an invalid drive name or invalid syntax on the command
line.

5
Disk write error occurred.

from Dos 6.22 help file which is the best reference for exit codes.

If you can find a way to get XCOPY to return an ERRORLEVEL of 1, you
get the prize.

ws
 
M

Matthias Tacke

David Candy said:
No. It's always been available. The if statement is more powerful in
NT but errorlevel is exactly the same.

You are right as long as

if errorlevel 1

is meant. You are wrong refering to the %errolevel% variable which may
be compared with lss gtr etc. see
if /?
 
D

David Candy

Which is why I said if was different. It's just a program exit code that a program can set when it terminates.
 
M

Matthias Tacke

David Candy said:
Which is why I said if was different. It's just a program exit code
that a program can set when it terminates.

Please explain the output of the following batch containing no if:

@echo off
for /L %%A in (1,1,16) do (
Call :RetErrLvl %%A
call echo %%errorlevel%%
)
goto :EOF
:RetErrLvl
exit /b %1
 
M

Matthias Tacke

David Candy said:
Explain what about it. It doesn't use comparison operators or if (or
set /a etc)
It uses the *variable* %errorlevel% what you seem to have problems to
understand. If handles the value like any other variable.

Note the presence of the percent signs. You replied:
No. It's always been available. The if statement is more powerful
in NT but errorlevel is exactly the same.

And in this context your answer is *not* true. The complicated handling
of errorlevels in a descending manner is obsolete under the newer win os
through the cmd shell handling the *variable* errorlevel in parallel.

Please read the output of "if /?" carefully. You will note the
difference between errorlevel and %errorlevel% and hopefuly understand
it this time.
 
D

David Candy

I used Dos 5 (built into XP) to echo an errorlevel. Didn't work, which is why most of my files use this
If errorlevel 6 if not errorlevel 7 Echo The error level was 6
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top