Windows batch: robocopy exclude files of specific subfolders

Hello experts,
I have A C:\Folder which contains multiple subfolders such as conf, lib, input, out, logs exports, etc.
I would like to launch robocopy command and exclude files of specific subfolders such as logs and out. The difficult part is that I need to exclude files of specific subfolders but keep the folder empty.
My attempt is.
 Robocopy C\Folder C:\Destionation /e /xf *.* 

Open in new window

The problem with this is that I exclude all files.
What I need is to exclude all files of specific folders and keep the folder involved empty.
If you have questions please contact me.
Thank you very much for your help.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vikas BhatExperienced IT Infrastructure Services/operations ManagerCommented:
robocopy C:\Folder C:\Destination /MIR /E /NP /V /R:2 /W:10 /XD "Path of folder to exclude" /XF "path of files to exclude" 
you can add multiple path of folders and files
robocopy C:\Folder C:\Destination /MIR /E /NP /V /R:2 /W:10 /XD "Path of folder to exclude" /XF "path of files to exclude" /XD "Path1 of folder to exclude" /XF "path1 of files to exclude" 

Open in new window

LD16Author Commented:
I am aware of /mir option the problem is that I don't want to create subfolders of subfolders I want to keepfolders to exclude empty and do it with the minimum of commands
The other think that I was thinking is to use /xd and then mkdir of folders to keep empty.
If another expert have another approach please don't hesitate.
Bill PrewIT / Software Engineering ConsultantCommented:
I don't understand the original question, perhaps an example?

Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

LD16Author Commented:
Hello Bill,
I have C:\Package which contains the following folders
I want to launch robocopy in order to copy all those subfoders to:
However I would like to exclude the content (files & folders) located at log and exports subfolders:
The aim is to have E:\Package with:
E:\Package\bin (with files and folders)
E:\Package\conf (with files and folders)
E:\Package\log (empty)
E:\Package\export (empty)

Let me know if it is clearer.
Vikas BhatExperienced IT Infrastructure Services/operations ManagerCommented:
Below should be able to do what you want:

robocopy C:\Package E:\Package /MIR /E /NP /V /R:2 /W:10 /XD "C:\Package\log" /XD "C:\Package\exports" 

Open in new window

Bill PrewIT / Software Engineering ConsultantCommented:
I don't see a way to do it in just one ROBOCOPY command.  There is a limitation to ROBOCOPY that wildcards are not allowed in the /XF option when a full path is used, which is what you really need.

You can get it done in two steps though.  The first will copy just the top level folder structure to the destination, no files.  Then the second will copy all the files except for the two folders you want to leave empty.  For example:

@echo off

set BaseDir=C:\Package
set DestDir=E:\Package

robocopy %BaseDir% %DestDir% /E /NP /V /R:1 /W:1 /XF "*.*"
robocopy %BaseDir% %DestDir% /E /NP /V /R:1 /W:1 /XD "%BaseDir%\log" "%BaseDir%\export" 

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LD16Author Commented:
Thank you very much Bill. For my knowledge. Could you please explain me  /np, /v, /r options.
This will help me to find the best way to manage issues with exclusions.
Bill PrewIT / Software Engineering ConsultantCommented:
/R:n :: number of Retries on failed copies: default 1 million.
/W:n :: Wait time between retries: default is 30 seconds.

These specify the number of retries if a file can't be accessed or problems happen copying it.  I tend to keep it low for most scripts, but you will see numbers like 5 here when in production, to allow a few retries before moving along.  If you don't specify anything it defaults to a huge number and just keeps retrying forever.  The /W specifies the pause between each retry.

/V :: produce Verbose output, showing skipped files.

This just provides slightly more output to be logged during the copy, useful when testing, etc.

/NP :: No Progress - don't display percentage copied.

Skips the dynamic progress calculations and display during copies.  Can speed up the copy slightly and when running production mode, often as a scheduled job, you really don't need / want this.

LD16Author Commented:
Thank you Bill. Very clear.
I am looking your proposal and I was wondering if logs and exports folders have subfolders. First command will also copy them? I am asking this because in first command there is just /xf and not /XD.
Bill PrewIT / Software Engineering ConsultantCommented:
Yes, all subfolders would also be created by the first ROBOCOPY command, is that what you want?

LD16Author Commented:
Yes, it is clear know. Thank you very much for your help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.