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...
according to this answer you can use
– phuclv – 2017-04-22T16:18:00.827fsutil 8dot3name strip directory
to remove the existing names, no need to rename themWhy does FindFirstFile find short names? – phuclv – 2017-06-12T02:34:27.560
Well, that's a pretty good explanation of why - but it seems like there isn't a way to then force the dir command to ignore the aspx files? – chris – 2011-01-28T14:17:19.630
1@chris: No. But you can either remove the 8.3 names from your system (will likely save you a lot of trouble later), or filter the
dir
output withdir /b/s *.asp | findstr /ile .asp
-- this discards lines that do not end with.asp
. – user1686 – 2011-01-28T14:19:55.097