How to copy folders and files with a list of extension but exclude a folder if it contains a certain name using a wildcard?

Viewed 30

I am trying to copy bulk files from one directory to another. I have a list of extensions am interested such as pdf, jpg, CSV, etc. I want to exclude the files if a sub-folder contains a certain name.

This code copies all the files and folders

ROBOCOPY A:\OLD\ C:\Users\AG\Documents\ /S *.doc *.docx *.pdf *.xlsx *.xlsm *.xlsb *.ppt *.pptx *.xls *.csv *.ne2 *.txt  *.JPG

But I want to exclude all the sub-folders with a certain name e.g. BACKUP at many levels, below are just some examples

A:\OLD\2011\BACKUP
A:\OLD\2012\SHARED\BACKUP
A:\OLD\2012\BACKUP\

I tried using a wildcard in the exclude option to robocopy but is not working

ROBOCOPY A:\OLD\ C:\Users\AG\Documents\ /S *.doc *.docx *.pdf *.xlsx *.xlsm *.xlsb *.ppt *.pptx *.xls *.csv *.ne2 *.txt  *.JPG
/XD *BACKUP* 

How do I get this to work or an alternative? Please help.

1 Answers

From the documentation of robocopy /?:

[...]
::
:: File Selection Options :
::
[...]
 /XF file [file]... :: eXclude Files matching given names/paths/wildcards.
 /XD dirs [dirs]... :: eXclude Directories matching given names/paths.
[...]

In contrast to the /XF option (to exclude files), wildcards are not mentioned for the /XD option, which are indeed not supported for it.

Therefore, you will have to remove them and to use XD BACKUP or /XD "BACKUP" in your command line:

robocopy.exe "A:\OLD" "%UserProfile%\Documents" *.doc *.docx *.pdf *.xlsx *.xlsm *.xlsb *.ppt *.pptx *.xls *.csv *.ne2 *.txt *.JPG /S /XD "BACKUP"
Related