As user grawity already said in their answer, it is because the wildcards used by cmd
's internal commands and several external ones also match against 8.3 file names, which are enabled by default.
To work around that, there are several ways, some of which I want to show you:
You can use findstr
to filter the file names returned by dir
:
dir /S /B /A:-D "*.asp" | findstr /IEC:".asp"
This does not even match files that have .asp
somewhere in their names (like file.asp.aspx
), nor does it exclude files that have .aspx
in their names (like file.aspx.asp
).
The switch /B
lets dir
return a pure list of file names/paths, which can be filtered properly. The option /A:-D
excludes any matching directories.
Alternatively, the where
command could be used, which handles wildcards differently than dir
:
where /R "." "*.asp"
The disadvantage is that this command also regards the content of the PATHEXT
variable, so this would also match a file that ends in .asp.exe
, given that .EXE
is contained in PATHEXT
; to avoid that, you could temporarily clear the PATHEXT
variable (by set "PATHEXT_BACKUP=%PATHEXT%" & set "PATHEXT="
) and restore it later (by set "PATHEXT=%PATHEXT_BACKUP%" & set "PATHEXT_BACKUP="
).
This answer by user Konrad shows a way to change the default behaviour of creating 8.3 file names of items with an extension with more than 3 characters, which in turn prevents most of such unintended matches. But unfortunately this does not change any current 8.3 file names. And of course there could theoretically be situations where the 8.3 file names are still matching although quite unlikely.
I have to admit that I did not yet test this...