Using Robocopy to exclude a file extension from the root directory

Karl picture Karl · Jun 30, 2011 · Viewed 27.9k times · Source

I have a directory that I want to copy to another directory using Robocopy.exe.

My plan is to exclude a few files from the root of the source directory. In fact, I'd like to ONLY exclude .html files from the ROOT of the directory.

The trick is that I'm currently using /E which is currently causing all subfolders to be processed too.

Therefore, the current outcome of my operation is that if I use:

/E /XF "*.html"

I'm going to exclude all HTML files site-wide.

Is there a way that I can keep copying all sub-folders, but also use XF to exclude .html files from the root?

Something like:

/E /XF "c:\releases\website_source\*.html"

Answer

ArchGriffin picture ArchGriffin · Sep 2, 2012

Ok, so for no real reason, I had to find a way to answer this.

I could only find a "decent" way using powershell and it is still messy.

So first, edit the following powershell to match your needs:

$files = Get-ChildItem c:\releases\website_source -Filter {*.html}
"/XF" > c:\temp\exclude.rcj
foreach ($f in $files) {$f.FullName >> c:\temp\exclude.rcj}

This creates a list of files after the /XF command in a robocopy "job" file. Then call your robocopy command as normal but add /job:c:\temp\exclude.rcj to the end of it. This will basically make a complex /XF for each root HTML file simpler to write in your script.

Note you can do the above with a batch file, but I'm better with powershell then batch for looping and such.

Yes I realize this is a somewhat dated question at this point, but I needed something to do.