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>