1

Here is the content of the directory:

λ dir /b "..\src\"
main.c
main.c_
main.c2
main.cpp
main.cpp_
main.cpp2

This works as expected:

λ dir /b "..\src\*.c"
main.c

This doesn't:

λ dir /b "..\src\*.cpp"
main.cpp
main.cpp_
main.cpp2

Why does this wildcard match main.cpp_ and main.cpp2?

What is a working solution to list only *.cpp files in a directory?

EDIT: it is not a duplicate of cmd has wildcard bug?

raphaelh
  • 137

3 Answers3

2

Because the extension is more than 3 characters long and you're using a command interpeter which has a lot of backward compatibility code for 16 bit apps. If you were to run the same command in PowerShell it would behave as expected.

Edit because i can't respond to comments

Yes, just run powershell.exe and then issue the same command, less the /b flag.

Edit 2

As far as i'm aware, no there would not be a solution unless you wanted to write your own dir program. And regarding powershell, it would be a good idea to start familiarizing yourself with it, as i'm sure Microsoft is trying to kill off the legacy DOS interpreter.

Matt
  • 345
1

Although indeed a bit weird, it works as specified (italic marking by me):

You can use wildcard characters (* or?), to represent one or more characters of a file name and to display a subset of files or subdirectories.

Asterisk (*): Use the asterisk as a substitute for any string of characters, for example:

dir *.txt lists all files in the current directory with extensions that begin with .txt, such as .txt, .txt1, .txt_old.

dir read*.txt lists all files in the current directory that begin with "read" and with extensions that begin with .txt, such as .txt, .txt1, or .txt_old.

dir read*.* lists all files in the current directory that begin with "read" with any extension.

The asterisk wildcard always uses short file name mapping, so you might get unexpected results.

agtoever
  • 6,272
  • It might be worth adding to your answer that dir /x ... will show both the short and long filenames. – DavidPostill Sep 10 '15 at 12:45
  • Regarding the last sentence, I wonder what happens when one turns off 8.3 short filenames in the file system? (fsutil behavior set disable8dot3 1) – paradroid Sep 10 '15 at 13:04
  • So can you tell me where on cmd has wildcard bug? there's a solution to the problem described here? How can you list *.cpp files in a directory? – raphaelh Sep 11 '15 at 12:07
  • The first answer says: "Use dir /x to show (and work with) short versions of filenames". So dir /x *.cpp should work (not tested). – agtoever Sep 11 '15 at 12:15
  • Except I don't want to show or work with short versions of filenames, I just want to list *.cpp files in a directory... λ dir /x "..\src\*.cpp" | find "/" 09/09/2015 17:52 51 605 main.cpp 09/09/2015 17:52 51 605 MAIN~1.CPP main.cpp_ 09/09/2015 17:52 51 605 MAIN~2.CPP main.cpp2 – raphaelh Sep 11 '15 at 12:49
-2

I've found a solution without using powershell:

λ dir /b "..\src\" | findstr /r /c:".*\.cpp$"
raphaelh
  • 137
  • Strange. I read your original post as a question, not as a problem. That's why I gave an answer instead of a solution to a problem... If this works for you, please accept your own answer, so this question will be closed. – agtoever Sep 10 '15 at 13:10
  • Sorry if I haven't been clear enough! I've found out that I cannot accept my own answer until 2 days. – raphaelh Sep 10 '15 at 13:11