Dir

Index

Displays a list of files and subdirectories in a directory.

Syntax:

DIR [drive:] [path] [filename] [/Switches]

Switches:

noneBy default, DIR displays:
  • a header comprising the disk's volume label and serial number;
  • a list of all files and subdirectories in the current directory in the order they are listed in the FAT (except those marked 'hidden' and/or 'system' - see ATTRIB for an explanation of these terms). Along with each file/directory is its size, date/time of last modification, and long file name;
  • a footer comprising the total number of files listed, their cumulative size, and the free space (in bytes) remaining on the disk.
/A [attributes] Displays files with/without specified attributes.
If attributes is not specified, all (including hidden and system) files and subdirectories in the specified directory are displayed.
Multiple attributes can be specified (don't separate with spaces) but only those files with all the specified attributes will be displayed, ie. works as AND rather than OR.

attributes (Using the "-" as a prefix specifies "not")

DDirectories
RRead-only files
HHidden files
AFiles modified since last back-up
SSystem files
/O [sortorder] List files in sorted order.
If sortorder is not specified, directories are listed alphabetically followed by files, also listed alphabetically.
Any combination of sortorder keys can be specified and files will be sorted in the order of the keys.

sortorder (Using "-" as a prefix reverses the order)

NBy name (alphabetic)
SBy size (smallest first)
EBy extension (alphabetic)
DBy date & time (earliest first)
GGroup directories first
ABy Last Access Date (earliest first)
/SDisplays files in the specified directory and all its subdirectories
/WWide list format. File and directory names are listed in 5 columns
/BBare format. Files and directories are listed in a single column without header, summary, or any details.
/LOutput is in lowercase.
/PPauses with each screenful of information. Press any key to see the next screen.
/VVerbose mode. This displays attributes, date last accessed, and disk space allocated for each file, in addition to the standard information.
/ZLong file names are not displayed in the file listing - but see "Limitations" below.
/C[H]This switch is documented in MSDos 6.2 Technical Reference as:
"Displays the compression ratio of files compressed using DoubleSpace, based on an 8K cluster size. The optional H switch displays the compression ratio of files compressed using DoubleSpace, based on the cluster size of the host drive. The /C[H] switch is ignored when used with the /W or /B switch".
(Presumably these switches are still valid in Dos 7.x when used on a FAT16 drive with DoubleSpace/DriveSpace, but I have been unable to test this).
/4Displays the date as four digits rather than two.
This switch is available in Y2K ready versions of Dos 7.x - ie. in Win95 after installing the Y2K update patch available from Microsoft's Y2K Web site and in Win98 as shipped.

Notes:

  1. DIR will only accept one path as a parameter - long file/directory names that include a space must be enclosed in inverted commas.

  2. Wildcards (* and ?) can be used to display a listing of a subset of files and subdirectories.

  3. DIR will only display files which have system or hidden attributes set by including the /A switch even if the file is specifically entered as a parameter. However, if a directory is specified as a parameter, a simple DIR will display all, even if the directory is supposed to be hidden. On the other hand, when the /S switch is used for searching through subdirectories, files (hidden or not) in hidden subdirectories are not displayed even if the /A switch is used. To list every file in every subdirectory (hidden or not) see XCOPY "Tricks".

  4. DIR can be used with the standard period (.) shortcuts to show higher level directories. If more periods are used than are required to show the root directory, the directories and files without extensions for the current directory are listed - except that if the current directory is the root directory, a double period gives the message "Invalid Directory", but three periods will give such a listing. Exactly the same listing can be obtained using "DIR *.".

  5. When using redirection to send DIR output to a file or another command, it is often useful to use /A:-D (to list only files), and /B (to avoid extraneous information such as files sizes, headers and summary information).

  6. To find all instances of a particular file in a directory branch or drive, specify the starting directory pathname and the file as the parameter and use the /S switch (to include subdirectories). This gives full details of each file - size, date, long and short name.

    A rather more compact list - certainly more amenable to being redirected - will result from including the /B switch. This list will contain the full path and filename only.

    A similar list but with short filenames (and attributes) can be had using ATTRIB /S filename. XCOPY /L can be used to provide file lists based on different filters and with different output formats.

  7. When the output of a DIR command is redirected via a pipe, a temporary file is created which is automatically deleted once the operation is completed. By default, this temporary file is located in the directory specified by the "TEMP" environmental variable but, if this is not specified or cannot be found, it will be created in the current directory of the current drive. If the DIR command also refers to the current directory, any such temporary files will be picked up and included in the listing. Note that the "TEMP" environmental variable is normally created as Win9.x loads and would only not exist if specifically removed.

  8. DIR switches can be preset using SET with the DIRCMD environment. When thus set, any switch can be temporarily reset on the command line by prefixing the switch with a "-" (minus sign).

Limitations

The undocumented /Z switch (no long names) would appear to have been not fully developed and has a couple of problems:

  1. It will only work if:
    • There is no path specified (ie. for the current directory in the current drive)
    • The path is specified as the root directory of any drive (eg. C:\, D:\, etc.)
    • The path is specified as the current directory of any drive by using the drive letter only (eg. C:, D:, etc.)
    • The path is specified as the parent directory using the .. notation (eg. DIR .. /Z)

    Any other syntax results in a "File Not Found" error message.

  2. The /Z switch is compatable with the /S switch to show subdirectories (as long as the above rules are followed) and all the files are shown with short names only. The subdirectories are also shown with short names only. However, the header for each subdirectory after the first level gives the subdirectory's long name.

  3. The /Z switch is also compatable with the /B switch to give a simple list of files with short names only. When used with the /S switch as well, all files are listed with their full paths. The file names themselves are all in short form, and the path of those files in the current directory are in short form, but the paths of any files in subdirectories are in long filename form.

Examples:

To display all files and directories in the current directory, including hidden or system files:
dir /a

To display the files in the data directory and its subdirectories in wide format:
dir /w/s

To print a directory listing, using the redirection symbol:
dir > lpt1

To redirect output of the DIR command to the file in the "records" directory:
dir > \records\dir.doc
(If "dir.doc" does not exist, it is created.)

To list the directories and any files without extensions in c:/data (the current directory):
dir... or
dir *.

File Details:

Internal


Thanks to Gerard Schildberger for: the information regarding the /4 switch; pointing out the function of the /Z switch; the existence of the /C[H] switches; and adding to the information about pipes (Note #6). Thanks to David Lobbestael for the notes on the /C[H] switch.
This page last revised:
March 8, 2002