Solved

XCOPY type copy to copy only non-existant files

Posted on 2007-11-16
26
3,467 Views
Last Modified: 2010-04-21
I know the /D switch in XCOPY will copy only files that don't exist in the destination path or newer files, but I need to exclude the option to copy newer files (what XCOPY needs is a /-U switch).

Anyone know how I can accoplish this without using the /-Y switch, but I am dealing with about 12,000 files, and prbably only a couple of hundred don't exist in the destination.
0
Comment
Question by:dbbishop
  • 15
  • 11
26 Comments
 
LVL 30

Expert Comment

by:SteveGTR
Comment Utility
From the command line you can do this:

for %a in (*.*) do if not exist "c:\destdir\%a" echo copy "%a" "c:\destdir"

From a batch file you'd say:

for %%a in (*.*) do if not exist "c:\destdir\%%a" echo copy "%%a" "c:\destdir"
0
 
LVL 30

Expert Comment

by:SteveGTR
Comment Utility
Please note that you much remove the echo from both commands to actually perform the copy. The echo is for testing.
0
 
LVL 15

Author Comment

by:dbbishop
Comment Utility
I guess I should have pointed out there are subdirectories under the main directory.

c:\source\
c:\source\dir1\dir1.1\dir1.2\...
c:\source\dir2\
c:\source\dir3\dir3.1\dir3.2\dir3.3\...
etc.
0
 
LVL 30

Expert Comment

by:SteveGTR
Comment Utility
How will the files in the subdirectory be moved?

source\dir1 c:\destdir
source\dir1\dir2 c:\destdir
or
source\dir1 c:\destdir\dir1
source\dir1\dir2 c:\destdir\dir1\dir2
0
 
LVL 15

Author Comment

by:dbbishop
Comment Utility
c:\some_path_to_source\source\...
will be copied to
d:\source\...
0
 
LVL 30

Expert Comment

by:SteveGTR
Comment Utility
Do you see my to examples using your examples?

Does either of these do what you want? Please answer:

a) no
b) yes the first 1st one does
c) yes the 2nd one does.

If no, I need some detailed examples using your examples posted at 02:52PM EST.

Thanks :)
0
 
LVL 15

Author Comment

by:dbbishop
Comment Utility
I am upping the points. I hate people who do what I did, not give a complete description of the task.

For one thing, your code would only copy to existing file structures, unlike XCOPY, which will create a directory if it does not already exist, however, as a last resort, I suppose I can use XCOPY with the /T switch first to make sure the source directory structure exists on the destination drive.

Secondly, you will only copy files in the 'root' directory from which the code was executed, not files in subdirectories.

C:\Source\myfile1.txt
C:\Source\myfile2.txt
C:\Source\Path1\myfile3.txt
C:\Source\Path1\Path2\myfile4.txt
C:\Source\Path3\Path4\Path5\Path6\myfile5.txt

If D:\PathA\Source was the destination folder, and the only file in it was myfile2.txt, myfile2.txt would not be copied, myfile1.txt would be copied, folders D:\PathA\Source\Path1\Path2, D:PathA\Source\Path3\Path4\Path5\Path6 would be created and the missing files would be copied into the correct folders.

If XCOPY had a /-U switch that worked exactly opposite of /U it would do exactly what I want.
0
 
LVL 30

Expert Comment

by:SteveGTR
Comment Utility
I appreciate the point increase. My pet peeve is when people don't answer questions. Did you see mine? Please answer it and I'll provide a solution.
0
 
LVL 15

Author Comment

by:dbbishop
Comment Utility
Sorry, I thought I did answer it.

 ..."your code would only copy to existing file structures, unlike XCOPY, which will create a directory if it does not already exist..."

"...you[rs] will only copy files in the 'root' directory from which the code was executed, not files in subdirectories."
0
 
LVL 30

Expert Comment

by:SteveGTR
Comment Utility
Here is my question again:

Do you see my to examples using your examples?

---- From previous post

How will the files in the subdirectory be moved?

source\dir1 c:\destdir
source\dir1\dir2 c:\destdir
or
source\dir1 c:\destdir\dir1
source\dir1\dir2 c:\destdir\dir1\dir2

---- End of previous post

Does either of these do what you want? Please answer:

a) no
b) yes the first 1st one does
c) yes the 2nd one does.

If no, I need some detailed examples using your examples posted at 02:52PM EST.

Thanks :)
0
 
LVL 15

Author Comment

by:dbbishop
Comment Utility
From above:

c:\some_path_to_source\source\...
will be copied to
d:\source\...

To make it clearer, if this was XCOPY, the command to copy everything would be

XCOPY c:\some_path_to_source\*.* d:\some_path_to_destination\ /s only I do not want to replace files already in the d:\some_path_to_destination\ branch (regardless of date).
0
 
LVL 30

Expert Comment

by:SteveGTR
Comment Utility
Do you see my question?

Can you just answer it?

Answer will be a, b, or c.
0
 
LVL 15

Author Comment

by:dbbishop
Comment Utility
I do not know what is so hard to understand about my answer. It will only work for the current directory, only when the destination directory already exists. So, I guess the answer is NO (or technically, a).

The first and second are exactly the same, one is just typing from the command line, the other is a batch file. If one wouldn't work, the other wouldn't, unless I am missing something.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 30

Expert Comment

by:SteveGTR
Comment Utility
You seem to misunderstand the relationship between the person who wants help and the person who gives help.

You asked a question and I answered it. You then expanded on the question, not something that is looked on with favor by EE.

I asked a specific question to help me help you and you don't answer it. I didn't count, but I believe I asked the specific question 3 times...

If you didn't understand the question then say so. You say that the 1st and 2nd are exactly the same. You are obviously confused. I'll take some of that and say that maybe I could have asked them question better.

Case 1:

  example 1: Move c:\filesource\dir1\file1.txt to c:\destdir
  example 2: Move c:\filesource\dir1\dir2\file2.txt to c:\destdir

Case 2:

  example 1: Move c:\filesource\dir1\file1.txt to c:\destdir\dir1
  example 2: Move c:\filesource\dir1\dir2\file2.txt to c:\destdir\dir1\dir2

Is either of these what you want? If not then please example with some specific examples.

Thanks :)

0
 
LVL 15

Author Comment

by:dbbishop
Comment Utility
I do not think I expanded my question, but clarified it. I said I want something that works like XCOPY but will not copy files that already exist. XCOPY will copy subdirectories. You sample batch file won'. If all I was interested in were files in a sinngle directory, I could use Pedigree Copy. I reply no to overwrite a file and select to use that option for all files. Copy done!

Your questioin is very confusing:

>> Here is my question again:
>> Do you see my to examples using your examples?


If the source is c:\sourcepath and destination is d:\destinationpath
then everything under c:\sourcepath would be copied to d:\destinationpath that did not already exist under d:\destinationpath. It is that simple. I do not know how I can make it any clearer than that.

My examples are very clear. If you cannot understand what I want to do I have no idea how I can make it any clearer. I have a root directory. Under it there can be many, many directories, and under each of them, one or more directories. I want to copy everything, including strucure where it does not exist, from the source a destination directory.
0
 
LVL 30

Expert Comment

by:SteveGTR
Comment Utility
I won't debate you on the adding on to the question other than to state you should word your specifications exactly. Don't leave out specifications or you'll run into problems. Some advice that may help you in the future.

Your refusal to even entertain my examples really distresses me. It's like working with a user who knows a little about computers and thinks they know it all. They refuse to listen to your questions. They what to give out information they think will be helpful.

It's a two way street. As I don't really read your responses as I see they don't even attempt to address my questions. You don't even attempt to read my questions. Two way street. Lots of wasted energy.

I'm going to go out on a limb and say that you want Case 2:

In this case you have the file c:\filesource\dir1\file1.txt that does not exist in c:\destdir\dir1. You want to then move it to c:\destdir\dir1.

I'll code that up for your review at no cost whatever to you...
0
 
LVL 30

Expert Comment

by:SteveGTR
Comment Utility
This code will not actually move anything. To do that, you should modify the set debug line as annotated in the code:
@echo off
 

setlocal enabledelayedexpansion
 

REM ** To enable processing remove Y from the following line

set debug=Y
 

if "%~2"=="" echo usage %0: sourceDir DestDir&goto :EOF
 

if not exist "%~1" echo %~1 does not exist&goto :EOF
 

pushd "%~1"
 

set srcDir=%cd%

set destDir=%~2
 

if "%destDir:~-1%"=="\" set destDir=%destDir:~0,-1%
 

for /f "tokens=*" %%a in ('dir /b /s /a-d *.* 2^>NUL') do call :PROCESS "%%a"
 

popd
 

goto :EOF
 

:PROCESS
 

set destFile=%~1

set destFile=%destDir%!destFile:%srcDir%=!
 

if exist "%destFile%" goto :EOF
 

echo Copying %~1 to %destFile%
 

if not "%debug%"=="" goto :EOF 
 

echo F|xcopy "%~1" "%destFile%" >NUL
 

goto :EOF

Open in new window

0
 
LVL 30

Expert Comment

by:SteveGTR
Comment Utility
My prior comment should have said copy instead of move:

"In this case you have the file c:\filesource\dir1\file1.txt that does not exist in c:\destdir\dir1. You want to then move it to c:\destdir\dir1."

Should read as:

"In this case you have the file c:\filesource\dir1\file1.txt that does not exist in c:\destdir\dir1. You want to then copy it to c:\destdir\dir1."
0
 
LVL 15

Author Comment

by:dbbishop
Comment Utility
Steve,

I am not ignoring your last post. I was hoping for a rather speedy solution. I am currently on vacation until Dec. 1st and will not be able to fully test your solution until my return. I will let you know the results after testing it.

Doug
0
 
LVL 30

Expert Comment

by:SteveGTR
Comment Utility
Have a great time :)
0
 
LVL 15

Author Comment

by:dbbishop
Comment Utility
Sorry I haven't gotten back to you. It is not forgotten. This is a personal project, and I came back from vacation into a literal smelting pot of products. One person on our team was laid off and another had resigned and I am bouncing off the walls. I will try this out and get back ASAP.
0
 
LVL 15

Author Comment

by:dbbishop
Comment Utility
Steve,

I am so sorry for taking so long to get back to you. I normally do not take anywhere this long to respond to questions. Shame on me.

Anyway, I tried out your code and nothing is getting copied. I tried it first with no destination directory structure in place to see if it would create the structure. No directories got created. So, I created the empty structure and tried it again. Still, no files got copied.

I did get an email from someone off-site suggesting I look into a program called xxcopy. I did check it out and it appears to do everything (and SO MUCH MORE) I am wanting to accomplish here. Unless you can come up with a quick fix here, I will probably go ahead and download and use it (btw, I did not mention it, but the directory structure I am wanting to update is rather large--currently, a total of 1,160 folders and over 15,000 files occupying over 65GB of data. Some of the directory structures are 5-6 deep from the root.)
0
 
LVL 30

Expert Comment

by:SteveGTR
Comment Utility
Did you enable the code as I directed?
0
 
LVL 30

Accepted Solution

by:
SteveGTR earned 250 total points
Comment Utility

@echo off

 

setlocal enabledelayedexpansion

 

REM ** To enable processing remove Y from the following line

set debug=

 

if "%~2"=="" echo usage %0: sourceDir DestDir&goto :EOF

 

if not exist "%~1" echo %~1 does not exist&goto :EOF

 

pushd "%~1"

 

set srcDir=%cd%

set destDir=%~2

 

if "%destDir:~-1%"=="\" set destDir=%destDir:~0,-1%

 

for /f "tokens=*" %%a in ('dir /b /s /a-d *.* 2^>NUL') do call :PROCESS "%%a"

 

popd

 

goto :EOF

 

:PROCESS

 

set destFile=%~1

set destFile=%destDir%!destFile:%srcDir%=!

 

if exist "%destFile%" goto :EOF

 

echo Copying %~1 to %destFile%

 

if not "%debug%"=="" goto :EOF 

 

echo F|xcopy "%~1" "%destFile%" >NUL

 

goto :EOF

Open in new window

0
 
LVL 15

Author Closing Comment

by:dbbishop
Comment Utility
Well, kind of works. Doesn't copy hidden or system files. I am going to go ahead and award points to you as you apparently spent some time on this and I don't think it would be fair not to. I will probably go ahead and use XXCOPY. Thanks for the help, and your code does do 95% of what I need.
0
 
LVL 30

Expert Comment

by:SteveGTR
Comment Utility
Hey, thanks a lot...
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now