Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Robocopy to exclude a file extension from the root directory

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"
like image 735
Karl Avatar asked Jun 30 '11 16:06

Karl


People also ask

How do I exclude a file type in robocopy?

The most important switches in this command are the /XD which allows you to exclude folders, and /XF that you can use to exclude files. The other options are optional, but you should use these options that you should use in any standard copy process using Robocopy.

How do I exclude multiple files in robocopy?

I preface them with "rc" (for robocopy), then some recognizable notation for the application or part of the file system in the robocopy command, then append "B" or "R" (for Backup or Restore), then "I" or "X" (for Include or Exclude), then "D" or "F" (for Directory or File).

Does robocopy skip files?

Robocopy normally overwrites those. :: With the Changed, Older, and Newer classes excluded, Robocopy will exclude files existing in the destination directory.

What is robocopy XD?

Robocopy allows you to filter items not just by file but by directory name too. Using robocopy /xd , you can exclude certain directories matching a specific name. When copying multiple folders, use the /XD switch to exclude folders from the run.


2 Answers

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.

like image 174
ArchGriffin Avatar answered Nov 04 '22 08:11

ArchGriffin


My solution would be indecent but very easy to understand. I would just perform the task with a two line batch file. Robocopy the root folder (but not subdirectories) - including .html files. Then next line Robocopy including all subs (excluding *.html)

like image 42
Raiser Avatar answered Nov 04 '22 08:11

Raiser