Wildcards

Maintained on

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

SymbolMeaningExample
*Any string of zero or more characters*.txt (all txt files)
?Any single characterreport?.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 *.doc can match unintended files. Check with dir first.

Wildcard Basics

* (asterisk)

* matches any string of zero or more characters.

Show
dir *.txt
Show
dir report*
Show
dir *log*
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>dir /b *log*
app.log
catalog.csv
login.txt
C:\users\user>

? (question mark)

? matches any single character. It is useful when you want to limit the number of characters.

Files
dir data?.csv
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>dir /b data?.csv
data1.csv
data2.csv
dataA.csv
C:\users\user>
おすすめ書籍※ 広告を含む場合があります
中小企業経営者のためのRPA入門 RPA導入を成功させる方法

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

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

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

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

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

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかる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.

About a ? at the end of a name

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

PatternWhat 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

Find
dir /s /b *.log

See

for details.

copy / del / move: Operate on many files

Copy
copy *.txt backup\
Delete
del *.tmp
Move
move cache* archive\

If the path contains spaces, enclose the whole path, wildcard included, in quotation marks.

Example
del "C:\My Files\*.tmp"

ren: Change extensions in bulk

Change
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 details.

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.

Show
for %f in (*.txt) do @echo %f
When using this in a batch file

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

for details.

if exist: Check for existence

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

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>echo *.txt
*.txt
C:\users\user>

echo does not interpret wildcards, so the text is displayed as is. Whether wildcards work depends on the command.

CommandWildcards
dir copy del move ren typeSupported
for (inside the parentheses)Supported
whereSupported
rmdir (rd)Not supported (combine with for /d)
cdNot supported
findNot supported in file names (combine with for)
findstrThe search pattern is a regular expression; file names accept wildcards

Not allowed in the middle of a folder path

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

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

You
dir *.doc
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>dir /b *.doc
letter.doc
report.docx
C:\users\user>

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.

Check with dir before deleting

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.

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

Show
dir /a *.txt

To delete hidden files with del, add /a, as in del /a *.tmp.

Summary Table

GoalExample
Filter by extensiondir *.txt
Filter by the beginning of the namedir report*
Match files that differ by one characterdir data?.csv
Find files without an extensiondir *.
Search including subfoldersdir /s /b *.log
Process files one at a timefor %f in (*.txt) do ...
Change extensions in bulkren *.txt *.bak

Quiz

Practice Problem

Which file name matches data?.csv?

Choose one answer.

  1. data1.csv
  2. data10.csv
  3. data.txt

Practice Problem

Who expands wildcards in Command Prompt?

Choose one answer.

  1. Command Prompt expands them and then passes the result to the command
  2. Command Prompt passes them as they are, and the command interprets them
  3. The Windows file system expands them automatically
#Command Prompt #Batch File #Wildcard #Command Line #Command