XCOPY options

  • Thread starter Thread starter Rich Pasco
  • Start date Start date
R

Rich Pasco

XCOPY is a powerful utility with many options, but there are
two things I can't figure out how to do.

First:
Is there an XCOPY option to copy from folder 1 to folder 2 just
those files which do not already have a namesake in folder 2?
This would be the dual of /U which copies just those files
which do already exist. I am currently using /-Y and holding
down the "n" key, but there must be a better way.

Second:
Is there an XCOPY option to delete from folder 2 those files
which do not exist in folder 1? I am currently using this:

pushd folder2
for %%f in (*.*) do if not exist folder1\%%f del %%f
popd

Can anyone suggest a better way?

Thanks in advance,

- Rich
 
Rich said:
XCOPY is a powerful utility with many options, but there are
two things I can't figure out how to do.

First:
Is there an XCOPY option to copy from folder 1 to folder 2 just
those files which do not already have a namesake in folder 2?
This would be the dual of /U which copies just those files
which do already exist. I am currently using /-Y and holding
down the "n" key, but there must be a better way.

Second:
Is there an XCOPY option to delete from folder 2 those files
which do not exist in folder 1? I am currently using this:

pushd folder2
for %%f in (*.*) do if not exist folder1\%%f del %%f
popd

Can anyone suggest a better way?

You could download xxcopy and use it's /clone feature.

Or... You could use this.

xcopy folder1\ folder2\ /d /y
for %%f in (folder2\*.*) do if not exist folder1\%%~nxf del %%f
 
Rich Pasco said:
XCOPY is a powerful utility with many options, but there are
two things I can't figure out how to do.

First:
Is there an XCOPY option to copy from folder 1 to folder 2 just
those files which do not already have a namesake in folder 2?
This would be the dual of /U which copies just those files
which do already exist. I am currently using /-Y and holding
down the "n" key, but there must be a better way.

I have a file of "n"s -- 10,000 lines of "n"s -- called n.txt in my
batch file directory on each machine.

xcopy blah blah <c:\bat\n.txt

You can use an editor to cut and past the n's or just use a
numeric for..in..do loop.
Second:
Is there an XCOPY option to delete from folder 2 those files
which do not exist in folder 1? I am currently using this:

pushd folder2
for %%f in (*.*) do if not exist folder1\%%f del %%f
popd

I do this one basically this way. If it gets more complicated then
I write a Perl script to do it -- usually by writing the batch file
that will do the work even though Perl can do it directly, or can
callout to command utilities.
 
Herb said:
I have a file of "n"s -- 10,000 lines of "n"s -- called n.txt in my
batch file directory on each machine.

xcopy blah blah <c:\bat\n.txt

LOL! That's cute. :-)

Thanks for the tip, I think. :-)

- Rich
 
Todd said:
You could download xxcopy and use it's /clone feature.

Who publishes xxcopy? I really wanted to do it with just
Windows tools.
Or... You could use this.

xcopy folder1\ folder2\ /d /y

That will copy all files, including replacing those which exist
with an older date. In my case, I don't want to replace existing
files, just leave them alone and copy only files *not* having a
namesake in the target folder.
for %%f in (folder2\*.*) do if not exist folder1\%%~nxf del %%f

How is that better than what I wrote?

- Rich
 
Rich Pasco said:
LOL! That's cute. :-)

Thanks for the tip, I think. :-)

- Rich

There needs to be a switch for NO. (/N is taken and /-Y is
needed for environment variable default overriding).
 
Rich said:
Who publishes xxcopy? I really wanted to do it with just
Windows tools.

Google is your friend.
That will copy all files, including replacing those which exist
with an older date. In my case, I don't want to replace existing
files, just leave them alone and copy only files *not* having a
namesake in the target folder.

Sorry, i misunderstood the reason you used the /-y.

How is that better than what I wrote?

I don't recall stating it was "better", but it is my preferred method.
Relying on changing to another directory which may have been renamed, moved
or deleted would result in processing files in whatever folder happened to
be current. My method is explicit as to what should be processed. These two
lines should do what you wanted.

for %%f in (folder1\*.*) do if not exist folder2\%%~nxf xcopy %%f folder2\
for %%f in (folder2\*.*) do if not exist folder1\%%~nxf del %%f
 
Hi Rich,
Rich said:
XCOPY is a powerful utility with many options, but there are
two things I can't figure out how to do.

First:
Is there an XCOPY option to copy from folder 1 to folder 2 just
those files which do not already have a namesake in folder 2?
This would be the dual of /U which copies just those files
which do already exist. I am currently using /-Y and holding
down the "n" key, but there must be a better way.
do you mean also not overwrite older files or overwrite them only, if newer?
Second:
Is there an XCOPY option to delete from folder 2 those files
which do not exist in folder 1? I am currently using this:
This definitively would be a task for robocopy, which can be found in the
Windows Server 2003 Resource kit tools here:
http://thesource.ofallevil.com/down...69-57FF-4AE7-96EE-B18C4790CFFD&displaylang=en
Also not overwriting existing files, if they are newer or same date as the
files from source directory.
Best greetings from Germany
Olaf
 
Rich Pasco said:
Who publishes xxcopy? I really wanted to do it with just
Windows tools.

Then you could try Robocopy, which can be found in various resource kits,
and may be installed by default in some more recent versions of windows.

/Al
 
Herb said:
I have a file of "n"s -- 10,000 lines of "n"s -- called n.txt in my
batch file directory on each machine.

xcopy blah blah <c:\bat\n.txt

For a short while I thought that an even better way would be to
pipe into xcopy the standard output of a program (call it n.exe)
which writes an infinite stream of lines of n's. That way, there
would always be one when needed.

n.exe | xcopy blah blah

Then I remembered that Windows doesn't implement true pipes, but
instead waits for the source program to complete before starting
the destination.

So my solution is no better than Herb's (in fact worse, as it has
to regenerate the file of n's each time).

- Rich
 
Olaf said:
Hi Rich,

do you mean also not overwrite older files or overwrite them only, if newer?

I mean not overwrite the older files. They are good enough,
if they exist at all.
This definitively would be a task for robocopy, which can be found in the
Windows Server 2003 Resource kit tools here:
http://thesource.ofallevil.com/down...69-57FF-4AE7-96EE-B18C4790CFFD&displaylang=en

Thank you for the link, instead of sending me to Google.

Best regards,

- Rich
 
Rich Pasco said:
For a short while I thought that an even better way would be to
pipe into xcopy the standard output of a program (call it n.exe)
which writes an infinite stream of lines of n's. That way, there
would always be one when needed.

n.exe | xcopy blah blah

Then I remembered that Windows doesn't implement true pipes, but
instead waits for the source program to complete before starting
the destination.

So my solution is no better than Herb's (in fact worse, as it has
to regenerate the file of n's each time).

My solution is an ugly hack but I made it a standard part of all my
machines years ago and so it "just works now" <grin>
 
Rich Pasco said:
For a short while I thought that an even better way would be to
pipe into xcopy the standard output of a program (call it n.exe)
which writes an infinite stream of lines of n's. That way, there
would always be one when needed.

n.exe | xcopy blah blah

Then I remembered that Windows doesn't implement true pipes, but
instead waits for the source program to complete before starting
the destination.


just a bit of a nit, here... While Windows *itself* may not implement true
pipes, in this case the fault lies with cmd.exe. Whether you consider
PowerShell pipes "true" or not is up to you, but I think most will concede
that PowerShell pipes are true to the concept than CMD.EXE pipes. Therefore
the limitation involved is imposed by Windows.

/Al

<snip>
 
Rich Pasco said:
XCOPY is a powerful utility with many options, but there are
two things I can't figure out how to do.

First:
Is there an XCOPY option to copy from folder 1 to folder 2 just
those files which do not already have a namesake in folder 2?
This would be the dual of /U which copies just those files
which do already exist. I am currently using /-Y and holding
down the "n" key, but there must be a better way.


In Windows 2000 and XP, the following should work:
replace \folder1\*.* \folder2\ /A


--
email me: change "nospam" to "w.hennings"
Wilfried Hennings c./o.
Forschungszentrum (Research Center) Juelich GmbH, MUT
<http://www.fz-juelich.de/mut/mut_home>
All opinions mentioned are strictly my own, not my employer's.
 
Wilfried said:
In Windows 2000 and XP, the following should work:
replace \folder1\*.* \folder2\ /A

Thank you!

Indeed it does work, it you only want to update one folder.
Replace prohibits the /A with the /S options.

- Rich
 
Rich Pasco said:
Thank you!

Indeed it does work, it you only want to update one folder.
Replace prohibits the /A with the /S options.

Thank you, too! Good to know the limitation.
I just never tried it with nested folders.


--
email me: change "nospam" to "w.hennings"
Wilfried Hennings c./o.
Forschungszentrum (Research Center) Juelich GmbH, MUT
<http://www.fz-juelich.de/mut/mut_home>
All opinions mentioned are strictly my own, not my employer's.
 
Back
Top