views:

468

answers:

2

Hi, I've been trying to create this script that basically mirrors two sets of folders and it's content (including subfolders), but only copy files that are newer than the ones that already exist. I tried doing this by writing a vbscript, but it turns out that I manualle need to scan each subfolder and then it's content. And then that subfolder's content as well and so on and so forth.

So I'm wondering if anyone knows if this can be done with Robocopy?

+1  A: 

In short - yes - this is nice and simple with RoboCopy. By default it only copies changed files anyway, and won't bother copying unchanged files. You just need to add one argument to get it to exclude files where the destination exists and is newer.

The syntax of RoboCopy is a little bit quirky, so you may want to read more from here.

You probably want something like this:

RoboCopy.exe %sourceDir% %targetDir%\ *.* /xo

%sourceDir% - your source directory

%targetDir% - your target directory

*.* - the files in the source directory to copy

/xo - exclude file if the destination already exists and is newer.

[Edit in response to comment]

If you are saying you want to remove files from the destination directory that no longer exist in the source directory, then I believe the switch /purge does this.

Rob Levine
Hi, this does work :)However, say if the source folder has files and folders that I just removed because I don't need them anymore? I just tried that and that file or folder is still in the destination folder. I should use some sort of mirror function I think. Just not wiping out the entire destination in the process. I could write a vbscript that does this as well though. Just checks the files and folders to see if the content match. And then use robocopy to copy newer files.
Kenny Bones
Are you saying you want to remove files from the destination if they no longer exist in the source?
Rob Levine
You either need to add /E to cover subdirectories, or use /MIR which is equivalent to /E /PURGE
aphoria
+1  A: 

ROBOCOPY %SOURCE_DIR% %TARGET_DIR% * /MIR

CORRECTION

ROBOCOPY %SOURCE_DIR% %TARGET_DIR% * /MIR /XO

aphoria
-1 The OP specifically said "but only copy files that are newer than the ones that already exist". /MIR does not do that - it mirrors the source into the target, overwriting target files even if newer. If the OP ran this, it would trash any destination files that were newer.
Rob Levine
@Rob Levine I forgot the /XO. Thanks for the comment, but a little time to correct my answer before you downvote would be nice.
aphoria
@aphoria - fair enough. downvote removed :)
Rob Levine
@Rob Levine Thank you, I appreciate that.
aphoria
This kinda works. But you see, it's setup like this:If user is member of group1, copy \\server\folder to c:\dest\If user is member of group2, copy \\server\folder2 to c:\dest\Guess what happens when the user is member of both? :) This is my problem.
Kenny Bones
Ok, so I just tried to remove the /MIR switch. And this results in kinda the behaviour I want. If the user is member of both groups, he gets both contents. BUT, if I remove a file or folder from the source path and then run the script, it doesn't remove it from the local path, naturally. So, perhaps I need to combine this with a separate vbscript function that basically compares files and folders and deletes accordingly?
Kenny Bones
@Kenny Bones You're getting into a pretty tricky situation here. What if \\Server\Folder and \\Server\Folder2 have folders with the same name?
aphoria
Yeah, there are no duplicate folder names :) I know this for sure cause the folders are set up not to collide in any way.
Kenny Bones