In previous lessons we looked at some of the commands used to manipulate directories, and at the DOS Wildcards and File Attributes. In this lesson we put some of these pieces together and learn how to use the DIR command like an expert.
DIR with Wildcards
The DIR command is generally used to get a listing of all of the files in given subdirectory. But it can do more. If the DIR command is given without any arguments, it returns a list of all files in the subdirectory. But with a few arguments it becomes a search tool that works much like the Windows Find tool. For instance, you can use wildcards to search for a file that matches certain characters: C:\>dir *.doc This command would return a list of all the files in the root directory with the DOC extension. C:\>dir memo?.* This command would return a list of all files in the root directory with names that begin with MEMO, have one additional character following in the name, and have any extension at all. You can use all of the DOS Wildcards we discussed in the last lesson to find specific files. This can be very handy in a large directory to check for files, or to make sure before using a DEL command with wildcards that you know exactly what you are deleting. Remember that DOS does not have a Recycle Bin, so when you delete a file it may very well be unrecoverable. (There are DOS utilities to “undelete” a file, but they are uncertain. Better to not make a mistake in the first place.)
DIR with Attributes
You can also use the DIR command with an Attribute argument to find files that match the attribute selected. The syntax for this is the DIR command, followed by a space, then /A:, which signals an attribute argument, followed by the specific attribute(s) you want to search for. You can enter as many attributes as you like in order, without leaving a space between them. There are 10 attributes you can specify:
- h = hidden
- -h = not hidden
- s = system
- -s = not system
- a = archivable files
- -a = already archived files
- r = read-only files
- -r = not read-only (i.e. editable and deletable) files
- d = directories only, no files
- -d = files only, no directories
So, if you wanted to find all the files you not yet backed up, you would use a command like this: C:\>dir /a:a Every file in the root directory with the archive bit set “on” would be returned by this command. C:\docs\>dir /a:-ar This would return every file in the C:\DOCS subdirectory that was read-only and already backed up. If you know about Boolean operators, what happens when you use multiple attributes is that the command is interpreted as a logical “AND” command, which means that every file returned has to satisfy all of the attribute specifications. C:\temp\>dir /a:h This would return every file in the C:\TEMP subdirectory that was marked hidden. Interestingly, if you used a simple DIR command, no hidden files would be displayed. But if you use the command shown above, they are displayed for you quite plainly. Marking a file as hidden in DOS is not a lot of security, in other words. Don’t rely on it that way. Sometimes, particularly in the root directory, you may need to look for the subdirectories you have established. If you use a simple DIR command, you will be presented with several screens of files and subdirectories all mixed up. But you can do this: C:\>dir /a:d This command will return only the subdirectories from the root directory, without displaying any of the files in the root directory.
DIR in Order
You can also display the results of your DIR command in order. The syntax for this is very similar to using attributes. You leave a space after the DIR command, or a space after any other switches, and enter “/O:”, followed by a selection, to put things in order. Here you have 12 possible options
- N = alphabetical order (by filename)
- -N = reverse alphabetical order (by filename)
- E = alphabetical order (by file extension)
- -E = reverse alphabetical order (by file extension)
- D = Order by date and time, earliest first
- -D = Order by date and time, latest first
- S = By size (increasing)
- -S – By size (decreasing)
- C = By DoubleSpace compression ratio lowest to highest (Version 6.0 only)
- -C = By DoubleSpace compression ratio highest to lowest (Version 6.0 only)
- G = Group directories before other files
- -G – Group directories after other files
So, if you wanted to see your directory results grouped by file extension, you might want to try: C:\>dir /o:e This will return a list of files put in alphabetical order of file extension. So all the *.DOC files will come before all of the *.EXE files, and they will come before all of the *.TXT files. But if you are looking for that file you were working on yesterday: C:\>dir /o:-d This will return a list of files with the most recent ones at the top of the list. If you need to clean up your hard drive because you are running out of space: C:\>dir /o:-s This will return a list of files with the largest ones at the top of the list.
DIR with Multiple Arguments
You can use multiple arguments in a DIR command to achieve fairly complex results. Remember that each argument has to be separated from its neighbors by a blank space on each side. C:\>dir /a:a /o:d /p This command would select only those files that have not yet been backed up (/a:a), display them in order by date, beginning with the oldest (/o:d), and display the results on your monitor one page at a time (/p). So you can really do some slick stuff with the DIR command once you master the use of these arguments and switches. In case you were wondering, anything that modifies a command is an argument. If it has a slash in front, it is a switch. So all switches are also arguments, but some arguments (e.g. path) are not switches. Try out some of these. The best way to get comfortable with these commands is to practice using them.