Copy script

Microsoft Windows server 2003 enterprise
May 18, 2010 at 17:42:56
Specs: Windows XP pro Sp3, 1G
I am trying to use a vbscript or batch file to compare 2 folders and if they match it will copy content from one to the other, can anyone help me please.

See More: Copy script

Report •

May 19, 2010 at 01:35:47
Does that mean if the folder NAMES match? Or what?

Helping others achieve escape felicity


Report •

May 19, 2010 at 02:31:13

I just want to compare a folder name at 2 location, if they
match then just move or copy the content of the one of the
location's sub folders and files to the other.

Report •

May 19, 2010 at 02:35:24
Have you considered a utility like WinMerge?

Report •

Related Solutions

May 19, 2010 at 12:57:28
if exist \test1\%1 if exist \work\%1 copy /y \work\%1 \test1\%1
use like (f/e, saved as kopy.bat): kopy myfile

Report •

May 19, 2010 at 19:05:16
How will this work if I have say about 31 folders to match, I have to enter each name?

Report •

May 19, 2010 at 20:44:05
it always helps to say these things at the beginning. I'm very
lazy, so i always undercode...
i will assume that you are distributing fresh data or software
to multiple directories, poss. over network.
@echo off & setlocal
for /f "tokens=*" %%a in ('dir /s /a-d %1') do copy /y c:\work\
%1 %%a
::--- end
see if that does what you need. It will try to copy over itself, but copy won't allow that anyway so no need to code it up.
You'll see an error unless you do 2>nul after the copy stmt.

Report •

May 20, 2010 at 06:09:48
Thanks maybe I try to explain more,
I have say about 30 folders within a directory.
I am going to create the same 30 folder names on another directory.
I just want to run a batch file or vbscript to compare the 2 directory and if the names match then copy all the content of that folder including subfolders and files to the new folder. Just moving the users data from one place to another.
Will have a go with your script if it does that, thanks

Report •

May 20, 2010 at 13:34:22
for /f %%a in ('dir /b /ad c:\target') do (
if exist c:\source\%%a xcopy /s c:\source\%%a c:\target\%%a

Report •

May 20, 2010 at 15:25:19
The following is what I understand from your description.

You want a batch file where you specify a source directory
and a target directory. If the source directory has the same
name as the target directory, then copy all the subfolders of
the source directory to the target directory.


Source: C:\parent1\dir1 (contains subfolders a, b, c)
Target: C:\parent2\dir1 (contains subfolders c, d, e)

Result: since dir1 is the same name, copy subfolders a, b
and c to C:\parent2\dir1. The target will now have subfolders
a, b, c, d, e. The files from the source's c folder will overwrite
those of the target folder's.

Example 2:
Source: C:\parent1\dir1
Target: C:\parent2\dir2

Here, the batch file will do nothing, because dir1 is a different
name from dir2.

Is this a correct interpretation of your requirements?

If so, the following might work (I haven't tested it):

if "%~n1" == "%~n2" xcopy /s /e %1\* %2

Report •

May 20, 2010 at 20:18:51 is a folder with sub folders and files etc
there is about 30 folders in each class 2a and 3a etc,.



New empty folders


So a script to compare directory 2009 and 2010, if it finds a match with the name then it will move all the files from the old to the new folder. So any matches of names will do this.

Will this work?

Report •

May 21, 2010 at 08:56:22
Apart from the question whether or not you can do the copy, consider the fact that there exist merging/synching features in some advanced copy-commands.

Or you can do something like this:

xcopy C:\folder1 C:\folder2 /D /S
xcopy C:\folder2 C:\folder1 /D /S

Doesnt matter what order you put the commands, only must is to nicely switch the directory names correctly.

Report •

May 21, 2010 at 18:19:15
Thanks but that way I need to put each name in the command, I have about 720 folders to move over. I thought there is some command to compare them and only if they match then move the files over.

Report •

May 22, 2010 at 05:39:54
I think we are still trying to understand your requirement. I
presume it is to move all students' home directories, each one
under the same student's name, but considering that each
student has moved up a class.

So is the requirement specification something like this:

For every folder X under C:\Y\2009,
where Y is another folder name,
if folder C:\2010\Y1\X exists,
where Y1 = Y+1,
then copy all contents of folder C:\2009\Y\X to C:\2010\Y1\X.

This Y->Y1 complication means it's probably not that simple
to do this.

Report •

May 22, 2010 at 18:36:28
Yes I am trying to move the students folders up a class, they were from year 2 and now moved to year 3 but they have spread out into different classes, once they are moved I will delete the whole 2009 folder. So if x is the same then move C:\2009\y\x\ contents and sub folders to C:\2010\y1\x\. I have a script to create all the students name and folders where they will go in the new classes in 2010 So just need to compare the student's names from the old classes and new classes and if they match then move their contents over to the new folder. Does that make sense now and can this be done?

Report •

May 23, 2010 at 07:20:29
I think the best way to do it would be to have a database of students, which shows for each student which class they were in last year and this year. You could then just read each record and directly move the files.

Nevertheless, here is something that may work. It will not work properly if you have two students with the same name by coincidence. It give one student's files to the other student, which could land you in trouble if they have any sensitive personal data.

It is also untested, so you should take a backup of all the directories first.

for /d %%a in ("\2010\3*") do for /d %%b in ("%a\*") do (
   for /d %%c in ("\2009\2*") do (
      if exist "%%c\%%~nxb\" xcopy /e "%%c\%%~nxb\*" "%%b"

Report •

May 23, 2010 at 18:49:28
I will give it ago but not sure what you mean if the students have the same name, since I have created new folders in a different directory with the same name so we can move them over from the old class to the new class, doing it manually one by one is so time consuming.

Report •

May 23, 2010 at 21:36:23
see if this does what you want, viewing the outputlogfile "test":
@echo off & setlocal
for /f "tokens=3,4 delims=\" %%a in ('dir /b /s /ad \2010') do (
for /f "tokens=3,4 delims=\" %%m in ('dir /b /s /ad \2009') do (
if %%b equ %%n xcopy \l \s c:\2009\%%m\%%n c:\2010\%%a\%%b>> test
>> test echo c:\2009\%%m\%%n will be copied to: c:\2010\%%a\%%b. All subdirectories incl.
::------ end
my understanding is currently: test level-three subdirectories
for same-name, and if-equal copy all over-to 2010 level 3 & down.
pls let me know if this even comes close. Lots of near-misses so far!
this whole thing makes me think of dimensional arrays. Maybe the tree structure could also be re-configured as a mulitidimensional arena.
f/e: cd c:\+
navs to the next level of the onion, one level up from root. Maybe a command like "vector" would list all directories at level 2 off root.
Then "attributes" can contribute a third dimension. Then "permissions" a fourth.
I'm sure someone's been there/done that, probably in the nixiverse ("find").

Report •

May 24, 2010 at 01:52:18
@kiwi_hk: "not sure what you mean if the students have the same name"

I mean, for every single student, let's say Eric Lee in class 3t, are you 100% sure that there isn't another student also called Eric Lee, perhaps in class 3a or any other class?

Report •

May 24, 2010 at 21:41:45
No there is not another student called eric.lee in class 3a, there are only 2 eric.lee which he was in his old class C:\2009\2a\eric.lee and now this year he has moved to C:\2010\3t\eric.lee, so we are just moving all children folders from 2009 to there 2010 new classes and I can create the empty folders in their new class and user names in 2010\ directory, there is no duplicate login users name since their login name is there folder names.

What I am planning to do is delete all login existing users names and keeping their old folders.

Then Create new login users names with their empty folders in 2010 directory eg. \2010\3t....

Then this part that we are working out is moving their old docs to their new folders. (2009 to 2010)

When this is done I will delete 2009 directory.

Hope this makes sense.

Report •

May 25, 2010 at 02:58:16
I think it should be possible to write a VBScript to move each user's home directories without deleting and re-adding them. But that's beyond my level of experience in that area. So, for the time being, you could try either my last batch file attempt or nbrane's one, and see if it helps.

Report •

Ask Question