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.