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
  • 360
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,402
-2

I've found a solution without using powershell:

λ dir /b "..\src\" | findstr /r /c:".*\.cpp$"
raphaelh
  • 137