7

Using robocopy command I need to copy all files, but exclude files with particular character in the filename? For examlple copy all .jpg files with filenames containing underscore _ in it.

I have tried this command, but it doesn't work:

Robocopy C:\SourceFolder C:\DestFolder ^[^_]+.jpg$

Could be something really simple I'm overlooking here, but what?

There is also /XF flag to exclude certain file types, but (how) can it be used to exclude filenames containing the underscore in the filename?

Community
  • 1
  • 1
J3FFK
  • 664
  • 3
  • 14
  • 32

1 Answers1

13

I don't think robocopy supports regular expressions, but it does support wildcards (that is, the asterisk *).

So you'd include these wildcards when telling it what files to exclude using the /XF flag.

robocopy *.jpg C:\source C:\dest /XF *_*.jpg

This works if the _ is at the beginning, middle, or end of the file.

If you have multiple characters to wanted to exclude (say, exclude files that have underscores (_) and dashes (-)) then just add another wildcard statement after the /XF flag. You can list multiple parameters there, separated by spaces.

So something like

robocopy *.jpg C:\source C:\dest /XF *_*.jpg *-*.jpg
romellem
  • 5,792
  • 1
  • 32
  • 64