Using Wildcards with DOS commands

Using Wildcards with the DIR Command

Let us assume that you have forgotten the name of the file you created on the C disk, but you remember that the primary name starts with D and has five or less characters. If you try to locate your file by giving the DIR/P or DIR/W command, the large number of files on your disk would probably confuse you further. Instead, you could try the command:

C>DIR C:D????.* <Enter>

This would list all the file names starting with D and comprising five or less characters in the primary name, and any extension.

In all probability, you would recognize your file from the output of the above command.

Similarly, if you wish to view a list of files on the C disk, with the extension .EXE, the command you would use is:

C>DIR *.EXE <Enter>

Using Wildcards with the COPY Command

Let us assume that you wish to copy all files from the C disk with the extension .COB to the A disk. Instead of repeating the command for each file with the extension .COB, you can give the following command:

C>COPY *.COB A: <Enter>

Thus, with a single command, any number of files (with the extension .COB) will be copied. Without the facility of wildcards, copying all the .COB files one at a time will be tedious.

Let us take another example. To copy all files with primary names beginning with S and an extension of two characters or less, the command would be:

C>COPY S*.?? A: <Enter>

Using Wildcards with the DEL Command

Just as wildcards can be used with the DIR and COPY commands, they can also be used with the DEL command. However, this should be used with great caution.

To delete all files on the C disk, with the extension TXT, the command would be

C>DELC:*.TXT <Enter>

Similarly, to delete files with primary names beginning with X and an extension comprising two characters ending with A, the command would be

C>DELC:X*.?A <Enter>

Before using the DEL command with wildcards, you must be absolutely certain that you wish to delete all the files that match the specified pattern. Otherwise, you could lose some files that you actually want to retain.

The command

C>DEL C:*.* <Enter>

should be given with extreme caution because this would delete all files in the active directory of the hard disk. When this command is issued, DOS asks you if you are sure you want to delete the files.

Using Wildcards with Defined Paths

Wildcards can also be specified in file names as part of a command while using defined paths. For instance, if the active directory is the root and you wish to copy all files in the directory COBOL (with the extension .COB), from the C disk to the directory COBOL on the A disk, the command would be:

C>COPY \COBOL\*.COB A:\COBOL\*.COB<Enter>

This command would ensure that all files with the extension .COB in the COBOL directory on the C disk are copied onto the A disk in the directory COBOL. Similarly, paths can be used along with wildcards in other commands too. For example, to view a list of file names with an extension of one character or none in the ATTEND directory on the A disk, the command would be

C>DIRA:\ATTEND\*.? <Enter>

Even though the active drive is the C drive, the above command would list all file names on the A disk, under the directory ATTEND whose file names have an extension of one character or none.

If you wish to delete all files in the subdirectory DUE under the directory ADVANCE on the hard disk, the command would be

C>DEL\ADVANCE\DUE\*.*<Enter>

Using Wildcards ? and * Together

If you specify the wildcard pattern as PA???.*, all files with primary names beginning with PA and comprising five or less characters, and with any extension, will match the pattern. Instead of using the pattern PA???.???, it is simpler to use PA???.*.

Some more examples of patterns where both the wildcards are used together are given below:

TR??.*This will match all files with primary name beginning with TR and comprising four or less characters, and any extension.
T*.??This will match all files with primary names beginning with T and comprising any number of characters (upto a maximum of eight). The extension has to be of two or less characters.
???K.*This will match all files with primary names ending with K and comprising exactly four characters and any extension.
Z???Y. C*This pattern will match all files with primary names beginning with Z and ending with Y and comprising exactly five characters. The extension has to begin with C and may comprise any number of characters (subject to a maximum of three).

When specified in the file name as part of a command, wildcards provide greater flexibility and enable operations on a selective group of files that match the wildcard pattern.

Using the * Wildcard

If you wish to operate on the files whose primary names begin with A, you could specify the wildcard pattern as A???????.???.

This would match all file names beginning with A. However, using ? here proves to be very cumbersome. Instead of using ?, you could use the wildcard character *. The wildcard * can replace eight or less characters in the primary name and three or less characters in the extension. This is quite unlike ? which replaces only one character. The wildcard pattern which could be used here is A*.*.

From our first example, the file names that would match this pattern would be:

ABC. EXEAPP.COB
ATTN.TXTANN.TXT
ANNUALTXTANNUALCOB
ANNUALEXEATTLCOB

Let us take another example to understand this better. If you want to work on all files that have the extension .EXE, the wildcard pattern that could be specified is *.EXE. All file names with a primary name of any length and with an extension .EXE would match this pattern.

Thus, the ? wildcard provides character by character substitution, while the * wildcard is suitable when the number of characters to be matched varies.

DOS ignores any character which follows the * wildcard in the primary name upto the dot that separates the primary name from the extension.

For example, if you use the wildcard pattern *C.TEM, it would match all files with the extension TEM and not just those whose primary names end with C. The character C is ignored by DOS since it follows the * wildcard.

Similarly, if any character follows the * wildcard in the file name extension, it is ignored by DOS. For example, PAYROLLS.

Using the? Wildcard

Consider the wildcard pattern ????.COB. The following files will match from the list shown earlier:

        APP.COB                ONE.COB

        PEN.COB                ATTL.COB

Each ? can be replaced by one character or none. Since there are four ?s in the wildcard pattern, all files which have four or less characters in their primary name are listed by DOS.

DOS interprets a question mark in the same way as a card player interprets a joker—as a card that can substitute any other card.

Other characters can be intermingled with wildcard characters. An example is the wildcard pattern A???.COB.

This wildcard pattern will match all file names with primary names beginning with A and having four or less characters and the extension .COB.

From the files listed earlier, the following file names would be matched:

        APP.COB                ATTLCOB

If you have a file named PAINT.BAT and the wildcard pattern you specify is PA??T.BAT, the file PAINT.BAT will be listed.

Let us assume you have a file named SMS.PW and you wish to check if this file exists on the disk by using the wildcard pattern S7MS.PW.

DOS will display a message saying that the file has not been found. This is because ? can be matched by one character or none if it is specified at the end of a file name. However, when ? is specified in the middle of a file name, it has to be matched by exactly one character. The following example will illustrate this better.

Assume you have the following files on your disk:

        SIMPSON.KNG                PASTE.PAM

        TWINK.CS                        TWILIGHT.HZN

        PALE.PAM

The wildcard pattern you specify is P???E.PAM. Here the file PASTE.PAM will be matched and not PALE.PAM, even though their extensions are the same, i.e. PAM, and their primary names begin and end with the same letters. This is because the three ?s in the middle of the wildcard pattern must be matched by exactly three characters, like in PASTE.PAM. Similarly, each ? wildcard that is followed by other characters in a pattern has.to be matched by exactly one character.

Need for Wildcards

Often, you may find that you need to operate on many files on a disk. Specifying one file at a time for one operation can be tedious. If these files have certain similarities in their file names, they can be referred to collectively using the wildcard facility provided by DOS. Suppose a disk contains the following files:

ABC. EXEANNUAL.COB
ATTN.TXTGO.EXE
APP.COBATTL.COB
ANN.TXTONE.COB
PEN.COBANNUAL.TXT

You may need to work on only those files whose primary name is less than, or equal to, four characters and whose file name extension is .COB. Instead of selecting these files one by one, you can use the facility of wildcards, whereby you can specify a wildcard pattern with a command. DOS will refer to those files whose names match the specified wildcard pattern. Let us look at how the wildcards ? and * are used in commands.