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?
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.
In other words: Robocopy considers two files to be the same based only on whether their last-modified time stamps and file sizes are identical and therefore skips copying in that event.
Source Options /S - Copy subfolders. /E - Copy subfolders including empty folders (useful for recreating a folder structure) /COPYALL - Copy all file info, such as attributes and timestamps. /MAXAGE & /MINAGE - Use these to filter the file ages, either maximums or minimums.
While these are good options, depending on the amount of data you have to transfer, the process can take a long time using File Explorer. If you want to copy a lot of files faster and more reliably, you need a better solution, such as Robocopy.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With