Wildcards With the DIR Command

November 11, 2009

Visualize having 273 files in one directory. A few of those files have their extension .doc, but most don’t. You’re only looking for files with the .doc extension. Wouldn’t it be nice to type the DIR command so that only the .doc files come up? You can do this using wildcards.

A wildcard is one of two special characters, asterisk (*) and question mark (?), that can be used in place of all or part of a filename, often to enable a command-line command to act on more than one file at a time. Wildcards work with all command-line commands that take filenames. A great example is the DIR command. When you execute a plain DIR command, it finds and displays all the files and folders in the specified directory; however, you can also narrow its search by adding a filename.

If you want to confirm the presence of a particular file in a particular place, this is very convenient. But suppose you want to see all files with the extension .txt. In that case, you use the * wildcard, like this: dir *.txt. A good way to think of the * wildcard is “I don’t care.” Replace the part of the filename that you don’t care about with an asterisk.

Wildcards also substitute for parts of filenames. This DIR command will find every file that starts with the letter a:

C:\>dir a*.*

We’ve used wildcards only with the DIR command, but virtually every command that deals with files will take wildcards.

%d bloggers like this: