WinXP dir command: 3 and 4 char extensions are the same?

3

I'm trying to recursively search a directory tree to find all of the .asp files using the following command:

dir *.asp /s

For some reason, this returns not only the asp files, but also the aspx files.

How can I force it to ignore the aspx files?

chris

Posted 2011-01-28T12:58:53.660

Reputation: 8 607

Answers

5

dir /x

For compatibility reasons, Windows generates a 8.3 name for every long file name created, and wildcard matching code (FindFirstFile()) checks both the original and shortened names. Use dir /x to see what short names are assigned to each file.

The "extension" part of a 8.3 name is always created by simply truncating the last extension to at most 3 characters: .aspx to .ASP


When using the NTFS filesystem, 8.3 name creation can be disabled system-wide using:

fsutil behavior set disable8dot3

However, this won't affect existing names. You will have to rename each file and then rename it back to its original name.


See also: Directory search in Windows Command Prompt shows incorrect output!

user1686

Posted 2011-01-28T12:58:53.660

Reputation: 283 655

according to this answer you can use fsutil 8dot3name strip directory to remove the existing names, no need to rename them

– phuclv – 2017-04-22T16:18:00.827

Why 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 with dir /b/s *.asp | findstr /ile .asp -- this discards lines that do not end with .asp. – user1686 – 2011-01-28T14:19:55.097

0

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...

aschipfl

Posted 2011-01-28T12:58:53.660

Reputation: 553

0

Interesting - seems to be a bug...

Considering, I would pipe it through find like so:

dir /s *.asp | find /i /v ".aspx "

Multiverse IT

Posted 2011-01-28T12:58:53.660

Reputation: 4 228

it's not a bug, as per grawity's answer. But why do you leave a space at the end of aspx? – phuclv – 2017-03-29T01:49:26.277

1@phuclv, the trailing space must be removed, because it becaome part of the search string otherwise; anyway, this approach would also exclude files that have .aspx somewhere in the middle of their names, like file.aspx.name.asp, which should actually be included... – aschipfl – 2018-11-28T13:07:36.453

0

Of course this varies according to what command interpreter's DIR command one uses. The DIR command in Take Command, for example, only matches long names by default and so doesn't exhibit this behaviour. (Matching of short as well as long names can be turned on for compatibility with CMD's DIR command.) So there are ways to address this that don't require FSUTIL and administrator privileges.

JdeBP

Posted 2011-01-28T12:58:53.660

Reputation: 23 855

0

If you have PowerShell, you can use:

dir *.asp -r | remove-item

If you don't have PowerShell, why not?

Peter Hahndorf

Posted 2011-01-28T12:58:53.660

Reputation: 10 677