Windows CMD: “dir *.cpp” wildcard matches “main.cpp_” and “main.cpp2”

cmd.exewildcardswindows

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?

Best Answer

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

  • Related Question