Exclude all variations of file extensions with Robocopy

1

Robocopy seems to sometimes automatically wildcard file extensions when filtering. For example, using ".m" gets "filename.m" and not "output.mat". However, ".txt" gets "filename.txt" but also "dummyfile.txt-long".

So far, I have this: robocopy "E:\WorkDirectory" "F:\BackupDirectory" *.txt /E

I've already learned I can exclude files specifically via "/xf *.txt2".

However, I'd prefer a more general solution... say, something that would work for ".txt" but exclude ".txt1, *.txt2, *.txt3, ..." without having to enumerate all of them. Thanks!

Dan C

Posted 2016-11-05T19:50:05.717

Reputation: 13

Answers

0

You have discovered one of the legacy banes from the days of DOS. ROBOCOPY is not adding a wildcard to the extension, rather it is matching on the short 8.3 name of the file.

Back in the day, file names were limited to a base name with max length 8, followed by an extension of max length 3. When long file name capabilities were added, a method was needed to enable old programs to continue to work with the long names. So every file that did not meet the 8.3 spec was (is) automatically given an alternate short name that does meet the spec.

The short names still live on to this day, and the operating system automatically matches wildcard filenames against both the long and short names.

So a file name like somename.txt-long becomes something like SOMEFI~1.TXT. You can see why that short name matches your file mask.

Unfortunately, there is no way to prevent ROBOCOPY (or any other native command) from matching the short name. Because 8.3 names cause problems like you are experiencing, many system administrators disable 8.3 names, especially within a corporate environment.

If your files are stored on an NTFS volume, and if you run the console as administrator, then you can disable 8.3 names with FSUTIL. This can be done system wide, or on a per volume basis. However, this only prevents new 8.3 names from being created - existing 8.3 names still persist. You can also use FSUTIL to strip existing 8.3 names, but there are potential registry issues that can cause problems. I've never used the command, so I don't know all the ins and outs.

Without disabling 8.3 names, the only solution I am aware of is to process the files using your own FOR /F loop, coupled with DIR /B piped to FINDSTR to eliminate files with extensions longer than 3. You also must provide the command to process each matching file individually. Something like:

for /f "eol=: delims=" %F in ('dir /b /a-d *.txt^|findstr /lie ".txt"') do @someCommand "%F" ...

The above would process all .txt files within the current directory. There may be a fair amount of adjustments to get the command to do exactly what you want. Also, percents would need to be doubled if you use the command within a batch script.

dbenham

Posted 2016-11-05T19:50:05.717

Reputation: 9 212