Amarundo
Amarundo

Reputation: 2397

Batch File to Selectively Copy Files To an Identical Folder Tree

I have 2 identical folder trees, let's call them C:\First and C:\Second, with many subfolders. C:\First has many XML files in many sub and sub-sub folders. C:\Second just has the folder tree created.

I want to go through all XML files in C:\First\* and put it in it's equivalent place in C:\Second.

But first I want to check if a file with the same name exists in the C:\Current folder (no subfolders there), in which case I will copy the one from C:\Current to the proper sub-sub-sub folder in C:\Second.

In other words, I want to copy the whole structure and files from C:\First to C:\Second but I want to take the latest version that may or may not exist in C:\Current. And, in C:\Current there are many files I don't care about.

Example: C:\Current has these files:

a.xml
b.xml
1.xml
c.xml
d.xml
e.xml
2.xml
f.xml
g.xml
3.xml

In C:\First I have a.xml, b.xml, c.xml, d.xml, e.xml, f.xml, g.xml spread out in its sub-folders.

I hope I'm not being too confusing...

Upvotes: 0

Views: 152

Answers (1)

Aacini
Aacini

Reputation: 67216

When a problem is properly stated, the same problem description may serve as specifications to write the program. In your case, you have this description:

I want to go through all XML files in C:\First* and put it in it's equivalent place in C:\Second.

But first I want to check if a file with the same name exists in the C:\Current folder

The "But first" doesn't serve to write a program. You just need to write the first things in first place! For example:

I want to go through all XML files in C:\First*. I want to check if a file with the same name exists in the C:\Current folder in which case I will copy the one from C:\Current to the proper sub-sub-sub folder in C:\Second. Otherwise put the file from C:\First in it's equivalent place in C:\Second.

The pseudo-code below is an example of how your original problem description may be translated into a program:

rem I have 2 identical folder trees, let's call them C:\First and C:\Second, with many subfolders.
rem C:\First has many XML files in many sub and sub-sub folders. C:\Second just has the folder tree created.

rem I want to go through all XML files in C:\First\* 
for /R inside C:\First with all *.XML files do (
   rem But first I want to check if a file with the same name exists in the C:\Current folder (no subfolders there)
   if exist "C:\Current folder\place here just the name of the file from the for command" (
      rem in which case I will copy the one from C:\Current to the proper sub-sub-sub folder in C:\Second.
      set properFolder=place here the sub-sub-sub folder from the for command
      set properFolder=change "C:\First" by "C:\Second" in properFolder
      copy "C:\Current folder\just the name" "!properFolder!"
   ) else (
      rem ... and put it in it's equivalent place in C:\Second. 
      set properFolder=place here the sub-sub-sub folder from the for command
      set properFolder=change "C:\First" by "C:\Second" in properFolder
      copy "the file from for command" "!properFolder!"
   )
)

If you analyze this code you will realize that the lines that get properFolder are the same in both parts, so a simpler method would be to get properFolder just one time before the if command.

You may use this pseudo-code as starting point to write your Batch file.

Upvotes: 1

Related Questions