Wildcards
suggest changeMany 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 clarification
Examples:
dir *.txt
- Matches
Myfile.txt
,Plan.txt
and any other file with the.txt
extension.
- Matches
dir *txt
- The period does not need to be included. However, this will also match files named without the period convention, such as
myfiletxt
.
- The period does not need to be included. However, this will also match files named without the period convention, such as
ren *.cxx *.cpp
- Renames all files with .
cxx
extension to have.cpp
extension.
- Renames all files with .
dir a?b.txt
- Matches files
aab.txt
,abb.txt
,a0b.txt
, etc. - Does not match
ab.txt
, since a question mark followed by a character other than a question mark or period cannot match zero characters. - Does not match
a.b.txt
, since a question mark cannot match a period.
- Matches files
dir ???.txt
- Matches
.txt
,a.txt
,aa.txt
, andaaa.txt
, among others, since each question mark in the sequence followed by a period can match zero number of characters.
- Matches
dir a???.b???.txt???
- Matches
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.
- Matches
dir ????????.txt & @REM eight question marks
- Matches the same files as
*.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 theren
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 thea.*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:
- Matches the same files as