Wildcards
Instead of naming files one by one, you can use a wildcard such as *.txt to refer to every file that matches a condition. It is a basic technique that works with many commands, including dir, copy, del, and ren.
This article explains what * and ? mean, how to use them with each command, and the pitfalls that often trip up beginners.
Summary
| Symbol | Meaning | Example |
|---|---|---|
* | Any string of zero or more characters | *.txt (all txt files) |
? | Any single character | report?.txt (report1.txt, reportA.txt, and so on) |
- The command itself expands the wildcard. Command Prompt does not.
- Wildcards cannot be used in the middle of a folder path (such as
C:\Us*\Documents). - A deletion like
del *.doccan match unintended files. Check withdirfirst.
Wildcard Basics
* (asterisk)
* matches any string of zero or more characters.
dir *.txt
dir report*
dir *log*
? (question mark)
? matches any single character. It is useful when you want to limit the number of characters.
dir data?.csv

中小企業経営者のためのRPA入門 RPA導入を成功させる方法

60分でわかる! AIエージェント 超入門

コマンドラインの黒い画面が怖いんです。

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門
data10.csv does not match because there is only one ?. For two characters, write data??.csv. Also, as the Info box below explains, data?.csv matches data.csv too.
A ? placed at the end of the name part matches “one character or zero characters.” For example, dir ab? matches ab as well as abc. A ? in the middle always matches exactly one character.
*.* and *.
| Pattern | What it matches |
|---|---|
*.* | All files (including those without an extension) |
*. | Files without an extension |
* | All files |
*.* does not mean “files that have an extension.” It matches all files, including those with no extension.
Examples by Command
dir: List and search
dir /s /b *.log
See
for details.copy / del / move: Operate on many files
copy *.txt backup\
del *.tmp
move cache* archive\
If the path contains spaces, enclose the whole path, wildcard included, in quotation marks.
del "C:\My Files\*.tmp"
ren: Change extensions in bulk
ren *.txt *.bak
With ren, you can also use wildcards in the new name to carry over the matching part of the original name. See
for: Process files one at a time
If you put a wildcard inside the parentheses of for, each matching file is assigned to the variable in turn.
for %f in (*.txt) do @echo %f
The example above is for typing directly at the command prompt. In a batch file, change %f to %%f.
Use for /d to target folders and for /r to search subfolders recursively. See
if exist: Check for existence
if exist *.txt echo There are txt files
See
for details.Cautions
The command itself expands the wildcard
Unlike bash and similar shells, Command Prompt does not expand wildcards. It passes them to the command as they are.
echo does not interpret wildcards, so the text is displayed as is. Whether wildcards work depends on the command.
| Command | Wildcards |
|---|---|
dir copy del move ren type | Supported |
for (inside the parentheses) | Supported |
where | Supported |
rmdir (rd) | Not supported (combine with for /d) |
cd | Not supported |
find | Not supported in file names (combine with for) |
findstr | The search pattern is a regular expression; file names accept wildcards |
Not allowed in the middle of a folder path
dir C:\Us*\Documents
A wildcard in the middle of a folder name is not interpreted as part of the path. To search across subfolders, use /s.
dir /s /b C:\Users\*.docx
Longer extensions can match (8.3 short names)
On systems where the file system generates 8.3 short names, a pattern can also match files whose extension is four or more characters long. This happens because the extension in a short name is truncated to three characters.
dir *.doc
If you use a pattern like del *.doc to delete files, .docx files may be deleted as well. Before any destructive operation, run dir with the same pattern and check the targets.
Before running del or move, run dir /b pattern with the same pattern and always check which files will be affected. Take extra care with del *.*, which deletes every file in the current folder (after a confirmation prompt).
How folders are handled
dir also lists folders that match the pattern, but del does not delete matching folders (it only targets files). To delete folders with a wildcard, use for /d as follows.
for /d %d in (tmp*) do rd /s /q "%d"
In a batch file, change %d to %%d here as well.
Hidden files
By default, dir and del do not target hidden or system files. To show them with dir, add the /a option.
dir /a *.txt
To delete hidden files with del, add /a, as in del /a *.tmp.
Summary Table
| Goal | Example |
|---|---|
| Filter by extension | dir *.txt |
| Filter by the beginning of the name | dir report* |
| Match files that differ by one character | dir data?.csv |
| Find files without an extension | dir *. |
| Search including subfolders | dir /s /b *.log |
| Process files one at a time | for %f in (*.txt) do ... |
| Change extensions in bulk | ren *.txt *.bak |