Many commands accept file name wildcards--characters that do not stand for themselves and enable matching of a group of filenames.
Wildcards:
*
: any sequence of characters?
: a single character other than a period (.
) or, if part of a sequence of question marks at the end of a maximum period-free part of a file name, possibly zero number of characters; see examples for clarificationExamples:
dir *.txt
Myfile.txt
, Plan.txt
and any other file with the .txt
extension.dir *txt
myfiletxt
.ren *.cxx *.cpp
cxx
extension to have .cpp
extension.dir a?b.txt
aab.txt
, abb.txt
, a0b.txt
, etc.ab.txt
, since a question mark followed by a character other than a question mark or period cannot match zero characters.a.b.txt
, since a question mark cannot match a period.dir ???.txt
.txt
, a.txt
, aa.txt
, and aaa.txt
, among others, since each question mark in the sequence followed by a period can match zero number of characters.dir a???.b???.txt???
a.b.txt
, among others. While the last question mark sequence is not followed by a period, it is still a sequence at the end of a maximum period-free part of a file name.dir ????????.txt & @REM eight question marks
*.txt
, since each file also has a short file name that has no more than 8 characters before .txt
.Quirk with short file names: the wildcard matching is performed both on long file names and the usually hidden short 8 chars + period + 3 chars file names. This can lead to bad surprises.
Unlike shells of some other operating systems, the cmd.exe
shell does not perform wildcard expansion (replacement of the pattern containing wildcards with the list of file names matching the pattern) on its own. It is the responsibility of each program to treat wildcards as such.
This enables such things as ren *.txt *.bat
, since the ren
command actually sees the *
wildcard rather than a list of files matching the wildcard.
Thus, echo *.txt
does not display files in the current folder matching the pattern but rather literally displays *.txt
.
Another consequence is that you can write findstr a.*txt
without fearing that the a.*txt
part gets replaced with the names of some files in the current folder.
Furthermore, recursive findstr /s pattern *.txt
is possible, while in some other operating systems, the *.txt
part would get replaced with the file names found in the current folder, disregarding nested folders.
Commands accepting wildcards include ATTRIB, COPY, DIR, FINDSTR, FOR, REN, etc.
Links: