4

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?

5 Answers 5

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

5
  • 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
    Jan 28, 2011 at 14:17
  • 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. Jan 28, 2011 at 14:19
  • according to this answer you can use fsutil 8dot3name strip directory to remove the existing names, no need to rename them
    – phuclv
    Apr 22, 2017 at 16:18
  • The behavior can vary between volumes: superuser.com/q/825615/213663#1806041 Aug 28, 2023 at 10:03
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.

0

Interesting - seems to be a bug...

Considering, I would pipe it through find like so:

dir /s *.asp | find /i /v ".aspx "
2
  • it's not a bug, as per grawity's answer. But why do you leave a space at the end of aspx?
    – phuclv
    Mar 29, 2017 at 1:49
  • 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
    Nov 28, 2018 at 13:07
0

If you have PowerShell, you can use:

dir *.asp -r | remove-item

If you don't have PowerShell, why not?

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .